Firebase消息服务未调用,仅调用一次 [英] Firebase Message Service not called but one time only

查看:84
本文介绍了Firebase消息服务未调用,仅调用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase消息服务来获取对我的Android应用程序的通知,但该服务未调用而是一次!收到消息后如何处理使其被调用? 这是我的代码:

I'm using firebase message service to get notification to my android app but the service not called but once! how to handle it to make it called whenever I got message? here is my code:

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

    String user_id = "0";

    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        user_id = remoteMessage.getData().get("from_user");

    }

    String click_action = remoteMessage.getNotification().getClickAction();
    Log.d(TAG, "Message data payload: " + click_action);

    //Calling method to generate notification
    sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle(), user_id, click_action);
}

private void sendNotification(String messageBody, String messageTitle, String user_id, String click_action) {
    Intent intent = new Intent(click_action);
    intent.putExtra("user_id", user_id);

    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,
            PendingIntent.FLAG_ONE_SHOT);


    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) 
getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
}

有我的有效载荷:

  const payload = {
    notification: {
        title: "New Friend Request",
        body: `${userName} has sent you a Friend Request`,
        icon: "default",
        click_action: "com.example.alaa.lapitchatapp.Target_Notification"

    },
    data:{
        from_user: from_user_id
    }
};

这是Mainfest中的服务:

This is the service in the Mainfest:

<service android:name=".FirebaseMessagingService">
    <intent-filter>
        <action 
 android:name=
 "com.google.firebase.MESSAGING_EVENT" 
      />
    </intent-filter>
</service>

推荐答案

经过太多的挖掘,我意识到了

After way too much digging, I've realised that

通知将发送到设备的系统托盘,数据有效载荷将在启动器活动的意图范围内发送.

因此,我应该处理将通知发送到的活动中的其他内容.答案在此处

So I was supposed to handle the extras from the activity in which I was sending the notification to. The answer was in here

因此,无需在onMessageReceived()内编写任何代码.

Therefore, there was no need to write any code inside the onMessageReceived().

感谢大家.真的很感激

这篇关于Firebase消息服务未调用,仅调用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆