ステータスバーの通知が機能しません...このガイドに従って、メソッドAsyncTask
から呼び出される次のコードを に作成しました。doInBackground()
private void newNotification(int count) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(handler.getContext());
builder.setContentTitle("New Notification");
builder.setContentText("Testing notification");
builder.setNumber(count);
// Creates an explicit intent for an Activity in your app
Intent intent = new Intent(handler.getContext(), MainActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(handler.getContext());
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) handler.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
handler
MainActivity
を実装するインターフェースを使用したgetContext()
への参照this.getContext()
です。MainActivity
パラメータ 1 で呼び出すと、なぜか何も起こりません。
@Override
protected String[] doInBackground(String... empty) {
newNotification(1);
return null;
}
デバッグは、コードのすべての行が実行されていることを示していますが、通知は表示されません。特別な権限についての言及や、UI スレッドから実行する必要があるという記述は見つかりませんでした。メッセージが到着したときのように、バックグラウンドサービスは通知することを目的としているため、そうではないと思います。
私が間違っていたアイデアはありますか?