android - 未调用 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() 上设置了一个调试点,但它没有被启动。
解决方案
我解决了这个问题。问题是在做了一些更改并构建了我们需要再次订阅主题的 android 项目之后。
推荐阅读
- python - 无法检测到来自类名的链接
- dynamics-365 - 禁用用户访问后,如何只允许用户查看 PO(采购订单)?
- node.js - 我想在查询中搜索包含搜索词的所有词
- python - 从 mongoengine 以 json 格式返回列表字段参考字段
- express - 到期后如何更新对象?Express / Typeorm / Type-Graphql
- shell - 需要从其他配置文件中读取值
- javascript - Javascript - 无法执行 catch 块进行测试
- google-cloud-platform - Gsuite/GCP Azure SAML 集成返回“无效电子邮件 - 我们目前无法处理您的请求,请稍后再试”
- php - 类别和标签的 Wordpress 分页问题
- ionic-framework - 无法在设备上进行 http 调用