私はプッシュ通知を表示するアプリケーションで作業しています。第三者のサービスを使用しないことはクライアントの要件であるため、GCM/Firebaseを使用することは問題になりませんのでご注意ください。アプリケーションがGCM/Firebaseなしで閉じるとAndroid通知を実行する
以下のコードを使用して、サービスからの通知を正常に表示できました。
public class SendNotificationService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
CharSequence title = "Notification Title";
CharSequence message = "This is a test notification.";
Drawable drawable= ContextCompat.getDrawable(this,R.drawable.brand_icon_color);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.brand_icon_small_color)
.setLargeIcon(bitmap)
.setAutoCancel(true)
.setContentTitle(title)
.setOngoing(false);
mBuilder.setContentText(message);
mBuilder.setTicker(message);
mBuilder.setWhen(System.currentTimeMillis());
NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
mBuilder.setContentIntent(pendingIntent);
notificationManager.notify(0, mBuilder.build());
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
Toast.makeText(this, "Notifications Stopped...", Toast.LENGTH_LONG).show();
}
}
私のAsyncTask onPostExecuteメソッドでこのサービスを開始しています。
Intent intentService = new Intent(context, SendNotificationService.class);
context.startService(intentService);
私はチュートリアルの数以下にこれを作成し、私はAndroidの設定で私の実行中のアプリに行く場合、私はこのサービスの実行を見ることができますことを見出しました。しかし、私はそのようなサービスを見つけることができませんでした。
問題は、アプリケーションを終了すると、通知も消えます。ユーザーが何らかの行動を取るまで、私はそれを残しておきたい。
さらに、このサービスは、アプリが起動していない場合でも電話の起動から開始します。
これを調べて、起動時にサービスを実行してください:http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on-boot-on-android – zed
「プッシュ通知を表示したい」 - コード内に何もこれと関係がありません。 「私の申請書を閉じるとき」 - 詳しく**、**あなたがこれを意味するものを説明してください。 – CommonsWare
コードで私は通知を表示することができます。このサービスをバックグラウンドで実行できる場合、この通知をプッシュする方法は難しいことではありません。アプリケーションを閉じるということは、単に最近のAppsからアプリケーションをスライドさせたり、Xをタップして閉じたりすると、通知バーから通知が消えてしまうことです。 –