【问题标题】:Firebase messaging service never calls onMessageReceived() in the new versionsFirebase 消息服务在新版本中从不调用 onMessageReceived()
【发布时间】:2019-04-10 12:15:09
【问题描述】:

我正在尝试发送带有通知的点击操作数据来处理它的 onClick 事件,但不幸的是,应用程序没有通过 onMessageReceived() 接收到任何数据。我尝试了很多东西,但都是徒劳的。

这是我的代码:

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


     String notification_title = 
remoteMessage.getNotification().getTitle();
    String notification_message = 
remoteMessage.getNotification().getBody();
    String click_action = 
remoteMessage.getNotification().getClickAction();
    String from_user_id = remoteMessage.getData().get("from_user");
    Log.v("user id", from_user_id);
    if(from_user_id!= null){
        Toast.makeText(this, from_user_id, Toast.LENGTH_SHORT).show();
    }
    NotificationCompat.Builder mBuilder = new 
NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(notification_title)
            .setContentText(notification_message)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

    Intent resultIntent = new Intent(click_action);
    resultIntent.putExtra("user_id", from_user_id);
    Log.v("user id", from_user_id);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntentWithParentStack(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(0, 
PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    int mNotificationID = (int)System.currentTimeMillis();

    NotificationManager mNotifyManger = 
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    mNotifyManger.notify(mNotificationID, mBuilder.build());

    }

这是我的主要活动:

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

这是我的毕业作品:

implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.google.firebase:firebase-database:16.0.3'
implementation 'com.google.firebase:firebase-storage:16.0.3'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.firebaseui:firebase-ui-database:4.2.1'
implementation 'com.google.firebase:firebase-messaging:17.3.4'

编辑: 这是我期待的数据:

const deviceToken = 
admin.database().ref(`/Users/${user_id}/device_token`).once('value');
return deviceToken.then(result => {
if (!result || !result.exists){throw new Error("Profile doesn't exist")}
const token_id = result.val();
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
    }
};

【问题讨论】:

    标签: android firebase firebase-realtime-database notifications firebase-cloud-messaging


    【解决方案1】:

    如果您的 RemoteMessage 包含 notificationonMessageReceived 仅在您的应用程序在后台时被调用。

    【讨论】:

    • 我已经在发送数据请检查我对我的问题所做的编辑,我认为问题出在我的 JavaScript 代码上,所以我试图记录远程消息以查看它是否即将到来.是的,我的应用在后台
    【解决方案2】:

    将通知部分移动到数据中。如果您从请求中设置了通知,FCM 使用系统通知托盘显示您无法控制的通知,另一方面,如果您需要处理推送响应,则应避免在请求

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

    【讨论】:

    • 我现在尝试了您的建议,但收不到通知。
    【解决方案3】:

    首先,您是否在有效负载中传递了您设备的 Firebase ID?如果是,请更新您提到的有效负载正文。我在那里看不到。 您的有效负载必须具有类似这样的 Firebase ID:

    "registration_ids":[
      "your device's firebase ID"
    ]
    

    "to":"your device's firebase ID"
    

    "token":"your device's firebase ID"
    

    其次,我不明白这段代码的意义:

    if (true) {
            Log.d(TAG, "Message data payload: there is not a problem");
        } else {
            // Handle message within 10 seconds
         Log.d(TAG, "Message data payload: there is a problem");
        }
    

    对于始终为真的 if 语句,else 的意义何在?

    第三,你在payload中给出的点击动作值——com.example.alaa.lapitchatapp.Target_Notification 这是您尝试通过单击通知打开的课程吗?

    【讨论】:

    • 我已经对真实代码进行了注释,只是尝试查看是否可以通过使用一些日志来调用该方法,但它从未调用过。是的,这是我点击通知时尝试打开的真实课程。我不知道你提到的这个id。你认为这可能是问题吗?
    • 这是我实际用来获取数据的代码,请检查编辑。提前致谢
    • 是的,我认为丢失的 ID(或令牌)可能是问题所在。如果没有任何给定的令牌或主题,您要向谁发送消息或通知?令牌或设备 ID 是标识消息接收方设备的注册令牌。这决定了消息被发送到哪个设备。请参考 firebase 文档 - firebase.google.com/docs/cloud-messaging/android/first-messagefirebase.google.com/docs/reference/fcm/rest/v1/…
    • 另外,如果你想使用 click_action 打开一个活动,那么仅仅提及类名不会导致它打开。您必须在有效负载中添加 click_action 并在应用程序中添加意图过滤器来处理它。请参考stackoverflow.com/questions/37407366/…medium.com/@Miqubel/… 了解更多信息。
    • 我已经传递了令牌 ID,但在另一个地方,现在的问题是该服务仅被调用一次,然后当我在发送其他通知时尝试调用它时没有发生任何事情。是的,我正在将 click_action 添加到引用的活动中,并使用意图来接收它并具有打开活动的操作。但也没有任何事情发生。也许这与火力基地有关,因为它只调用一次?我不知道。 @DerrylThomas
    【解决方案4】:

    在对 firebase 文档进行了大量挖掘之后,我找到了问题的答案。

    你会在那里找到它: Firebase Message Service not called but one time only

    谢谢大家。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 2018-01-22
      • 1970-01-01
      • 2016-10-09
      • 2021-06-05
      相关资源
      最近更新 更多