设备即使在日志中成功发送也未收到通知

时间:2017-09-19 13:23:36

标签: android firebase firebase-cloud-messaging

我正在使用firebase云消息传递服务创建一个简单的消息传递应用程序但是我正在使用云功能来处理通知,但无论何时我测试它总是说在日志中成功但设备什么都没有收到

以下是使用的云功能:

exports.sendNotifications = functions.database.ref('/meesages/{messageId}').onCreate(event => {
  var eventSnapshot = event.data;
  var str1 = "Sender : ";
  var str = str1.concat(eventSnapshot.child("messageOwner").val());
  console.log(str);

  var topic = "Messaging";
  var payload = {
      notification: {
          Message: eventSnapshot.child("messageText").val(),
          Sender: eventSnapshot.child("messageOwner").val()
      }
  };

  // Send a message to devices subscribed to the provided topic.
  return admin.messaging().sendToTopic(topic,payload)
      .then(function (response) {
          // See the MessagingTopicResponse reference documentation for the
          // contents of response.
          console.log("Successfully sent message:", response);
      })
      .catch(function (error) {
          console.log("Error sending message:", error);
      });

});

这是负责处理Android设备上的通知部分的类:

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            showNotification(remoteMessage.getData().get("Sender"), remoteMessage.getData().get("Message"));
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {

            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

        }

    }

    @Override
    public void onDeletedMessages() {
        super.onDeletedMessages();
    }

    private void showNotification(String Message, String Sender) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setContentTitle("New message : " + Message)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText("By : " + Sender)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

是的,我确保在mainactivity类中添加主题订阅

FirebaseMessaging.getInstance().subscribeToTopic("Messaging");

那么这里究竟是什么错误?

1 个答案:

答案 0 :(得分:0)

onMessageReceived()中的代码期望消息具有数据有效负载。这在the documentation中进行了解释,其中包括三个选项卡,显示通知,数据和组合的有效负载。将notification更改为data

  var payload = {
      data: {
          Message: eventSnapshot.child("messageText").val(),
          Sender: eventSnapshot.child("messageOwner").val()
      }
  };