2

アプリがバックグラウンドでデータペイロードのみである場合に通知を受け取りたい。 "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

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つのスクリーンショットで表示されます。

When App in BackgroundWhen app in foreground

どのように私はそれが私のアプリは、データペイロードを持つバックグラウンドで動作しているときに通知を得るために働くことができますか?または何か間違っていますか?誰かが解決策を教えたり、私を案内してください。

+0

このタイプの質問はスタックオーバーフローで何度も尋ねられますが、私が望む情報は見つかりませんでした。可能であれば誰かが私を解決策に導いてください。 – Sp4Rx

答えて

0

これは私が作った非常にばかげたばかげたミスでした。私はデータのペイロードしか送信していませんでしたが、私は通知データに何もない場合、自分のコードを実行しないことを意味するremoteMessage.getNotification() != nullをチェックしていました。条件は次のようにする必要があります。

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    super.onMessageReceived(remoteMessage); 

    if (remoteMessage.getData().size() > 0) { 
    //Do stuffs to show notification with data payload only 
    } 
} 

すべての通知が予期した通りに機能しています。

これは他のユーザーを対象としています。

通知とデータペイロードの両方を個別にチェックしたい場合は、このような両方の条件を含めることができます。

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    super.onMessageReceived(remoteMessage); 

    if (remoteMessage.getData().size() > 0) { 
     //Do stuffs with data payload only 
    } 

    if (remoteMessage.getNotification() != null) { 
     //Do stuffs with notificaiton data 
    } 
} 
関連する問題