当应用程序处于后台状态时,FCM多次推送通知无法正常工作

问题描述 投票:2回答:2

我使用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>
android firebase push-notification firebase-cloud-messaging android-notifications
2个回答
0
投票

消息数据消息和通知消息有两种类型。无论应用程序位于前台还是后台,都会在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);
        }

0
投票

您应该使用通知类型消息来处理所有情况。应用程序被杀/后台时,数据类型不起作用。因为对于数据类型通知,将调用OnMessageRecieved,您必须使用NotificationBuilder手动创建状态栏通知。

所以总是更好地使用服务器的通知类型输入,如下所示

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

        }

这将适用于所有情况,并且您不必担心在代码上处理它。

© www.soinside.com 2019 - 2024. All rights reserved.