アプリがバックグラウンドでデータペイロードのみである場合に通知を受け取りたい。 "notification" : {"sound": "default"}
アプリがバックグラウンドのときにFirebase通知を取得する(RESTクライアントを使用するデータペイロードのみ)
のような通知パラメータを送信する必要はありません。ここにメッセージを受信して通知を送信するコードです。
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String URL_KEY = "url";
private static final String TITLE_KEY = "title";
private static final String BODY_KEY = "body";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getNotification() != null) { //Here I made the dumb mistake
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
// Notification Data initialization
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// String notificationTitle = remoteMessage.getNotification().getTitle();
String notificationTitle = remoteMessage.getData().get(TITLE_KEY);
// String notificationBody = remoteMessage.getNotification().getBody();
String notificationBody = remoteMessage.getData().get(BODY_KEY);
String receivedUrl = remoteMessage.getData().get(URL_KEY);
// BuildNotificaiton
Intent internetIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(receivedUrl))
.setComponent(new ComponentName("com.example.suvajit.webview", "com.example.suvajit.webview.MainActivity"))
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
Random generator = new Random();
PendingIntent btn1Intent = PendingIntent.getActivity(this, generator.nextInt(), internetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
.setSmallIcon(R.mipmap.ic_launcher)
.setSound(defaultSoundUri)
.setAutoCancel(true)
.addAction(R.mipmap.ic_launcher, "Previous", btn1Intent) // #0
.addAction(R.mipmap.ic_launcher, "Pause", null) // #1
.addAction(R.mipmap.ic_launcher, "Next", null) // #2
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
}
これが私のマニフェストです:
<service android:name="com.example.suvajit.webview.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="com.example.suvajit.webview.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
そして私はARC
を使用して通知を送信しています。しかし、私は唯一のデータペイロード通知が受信されない送信していたときにアプリが前景にある場合でも。
{ "data": {
"url": "https://www.google.com",
"body" : "This is your message",
"title": "v-comply",
}
"to" : "dOa...hXIWnms"
}
私はsound
を送信するか、他の通知パラメータとして任意のものとアプリがフォアグラウンドで通知がそれが必要として働いていたがときにバックグラウンドでその示す唯一の私のプロジェクト名、データ部分されています私は受け取っていないと思います。
{ "data": {
"url": "https://www.google.com",
"body" : "This is your message",
"title": "v-comply",
},
"notification" : {
"sound": "default"
},
"to" : "dOa...hXIWnms"
}
アプリがバックグラウンドとフォアグラウンドでそれぞれ2つのスクリーンショットで表示されます。
どのように私はそれが私のアプリは、データペイロードを持つバックグラウンドで動作しているときに通知を得るために働くことができますか?または何か間違っていますか?誰かが解決策を教えたり、私を案内してください。
このタイプの質問はスタックオーバーフローで何度も尋ねられますが、私が望む情報は見つかりませんでした。可能であれば誰かが私を解決策に導いてください。 – Sp4Rx