簡體   English   中英

當應用程序處於后台狀態時,FCM多個推送通知無法正常工作

[英]FCM Multiple push notifications not working properly when app in background state

我正在使用fcm進行推送通知,僅處理數據有效負載。 當應用程序處於前台時,多個通知可以正常工作,這里我使用click_action中來自服務器的id值將通知移到相關帖子上。但是,當應用程序處於后台/關閉狀態時,只要我收到多個通知,我將點擊一個通知,它將轉到相關的帖子,但其余所有都不會重定向到相關的帖子,只是從通知中清除了。 我不知道為什么會發生這個問題,這是我的代碼

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d(TAG, "From: " + remoteMessage.getFrom());


        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"),remoteMessage.getData().get("click_action"),image);
        }

private void sendNotification(String title, String message, String id, String image) {
    Log.d("notificationdetails",title+",,,"+message+",,"+id);
    Intent  intent = null;



int postid = Integer.valueOf(id);
        if(id.equals("")|| id==null) {
            intent = new Intent(this, SplashActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        }

else{

        Constants.PushId = postid;
        intent = new Intent(this, DetailActivity.class);
        Log.d("mypostid",postid+"");
        intent.putExtra("id", postid);
        intent.putExtra("backpage","main");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    }

    PendingIntent pendingIntent = PendingIntent.getActivity(this, postid, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher_primepost)
            .setContentTitle(title)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);


    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Random random = new Random();
    int num = random.nextInt(99999-1000)+1000;
    notificationManager.notify(num /* ID of notification */, notificationBuilder.build());
}

}

的Manifest.xml

 <activity android:name="com.primepostnews.app.Activities.DetailActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="Detail" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

    </activity>

消息有兩種類型:數據消息和通知消息。 無論應用程序是在前台還是在后台,都在onMessageReceived中處理數據消息。 數據消息是GCM傳統上使用的類型。 當應用程序在前台時,僅在onMessageReceived中在此處接收到通知消息。 當應用程序在后台運行時,將顯示自動生成的通知;當用戶點擊通知時,他們將返回到應用程序。 同時包含通知和數據有效負載的消息被視為通知消息。 Firebase控制台始終發送通知消息。

 if (remoteMessage.getData() != null && remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"),remoteMessage.getData().get("click_action"),image);

        } else if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getBody(),remoteMessage.getData().get("click_action"),image);
        }

在所有情況下,您都應該使用通知類型的消息來工作。 當應用程序被殺死/在后台運行時,數據類型不起作用。 因為對於數據類型通知,將調用OnMessageRecieved,因此您必須使用NotificationBuilder手動創建狀態欄通知。

因此,始終最好使用如下所示的來自服務器的通知類型輸入

"notification": {
            "title": "Firebase notification",
            "message": "I am firebase notification. you can customise me. enjoy",
            "click_action": "OPEN_ACTIVITY",
            "sound":"default",

        }

這在所有情況下都適用,並且您不必擔心在代碼中處理它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM