首页 > 解决方案 > 未调用 FirebaseMessagingService 中的 onMessageReceived 方法

问题描述

我正在使用 firebase 云功能发送通知。当我使用通知键发送有效负载时,通知发送但未调用方法。但是有了数据键,什么都没有发生。

这是我的 FirebaseMessagingService 代码

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {


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

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

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


            if (/* Check if data needs to be processed by long running job */ true) {
                // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
                scheduleJob();
            } else {
                // Handle message within 10 seconds
                handleNow();
            }

        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
        }

    }

    private void sendNotification(String title, String messageBody){

        String channelId = getString(R.string.default_notification_channel_id);

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_logo)
                        .setContentTitle(title)
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0 , notificationBuilder.build());

    }

这是我的firebase云功能

exports.sendNotification = functions.database
  .ref("/deals/{userId}/{dealId}")
  .onCreate((data, context) => {
    const dataValue = data.val();

    const topic = context.params.userId;

    const payload = {
      data: {
        title: "New deal created",
        body: dataValue.user.name + " added a new deal."
      },
      topic: topic
    };

    // Send a message to devices subscribed to the provided topic.
    return admin
      .messaging()
      .send(payload)
      .then(response => {
        // Response is a message ID string.
        console.log("Successfully sent message:", response);
      })
      .catch(error => {
        console.log("Error sending message:", error);
      });
  });

我还在 onMessageReceived() 上设置了一个调试点,但它没有被启动。

标签: androidfirebasepush-notificationfirebase-cloud-messaging

解决方案


我解决了这个问题。问题是在做了一些更改并构建了我们需要再次订阅主题的 android 项目之后。


推荐阅读