【发布时间】: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