当他们从朋友那里收到消息时,我正在尝试向设备发送通知。但是,如果他们将要收到弹出窗口,则该应用程序会在出现消息之前自动转到首页。我就是不明白。消息既是数据类型又是通知类型。我只是不知道错误是从哪里来的。请帮忙。下面是我的代码。
JS代码。
exports.sendNotification8 = functions.database.ref('/Users/{user_id}/Notifications/{notifications_id}')
.onWrite((change,context) =>{
var user_id = context.params.user_id;
console.log(user_id);
// Grab the current value of what was written to the Realtime Database.
var eventSnapshot = change.after.val();
var device_token = admin.database().ref('/Users/'+user_id+'/device_token').once('value');
return device_token.then(result => {
var token_id = result.val();
console.log(token_id);
var str = eventSnapshot.message;
console.log(eventSnapshot.from);
var payload = {
notification: {
title: eventSnapshot.from,
body: str,
click_action: "Chats",
} ,
data: {
from_user_id: eventSnapshot.from_user_id,
name: eventSnapshot.from,
user_id: user_id
}
};
// Send a message to devices subscribed to the provided topic.
return admin.messaging().sendToDevice(token_id, payload).then(function (response) {
// See the MessagingTopicResponse reference documentation for the
// contents of response.
console.log("Successfully sent message:", response);
return;
})
.catch(function (error) {
console.log("Error sending message:", error);
});
});
});
我的onMessageReceived
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
String name = remoteMessage.getNotification().getBody();
String title = remoteMessage.getNotification().getTitle();
String click_action = remoteMessage.getNotification().getClickAction();
String from_user_id = remoteMessage.getData().get("from_user_id");
String texter_name = remoteMessage.getData().get("name");
String user_id = remoteMessage.getData().get("user_id");
showNotification(from_user_id, texter_name, user_id, name, title, click_action);
}
// Check if message contains a notification payload.
}
private void showNotification(String name, String title, String click_action, String from_user_id, String texter_name, String user_id) {
String GROUP_KEY_WORK_EMAIL = "com.dreamlazerstudios.gtuconline";
Intent resultIntent = new Intent(click_action);
resultIntent.putExtra("from_user_id", from_user_id);
resultIntent.putExtra("texter1", texter_name);
resultIntent.putExtra("user_id", user_id);
Intent intent;
switch (click_action) {
case "Download":
intent = new Intent(this, Download.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
break;
case "Download_Ent":
intent = new Intent(this, DownloadEnt.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
break;
case "InfoSys":
intent = new Intent(this, MIS_Information.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
break;
case "EntSystemsInfo":
intent = new Intent(this, EntSystemsInfo.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
break;
case "DatabaseInfo":
intent = new Intent(this, DatabaseInfo.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
break;
case "IntroToOil":
intent = new Intent(this, DownloadIntro.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
break;
case "IntroInfo":
intent = new Intent(this, IntroToOilInfo.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
break;
case "Student_SystemsDevt":
intent = new Intent(this, student_mis.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
break;
case "Chats":
intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
break;
default:
intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
break;
}
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(name)
.setAutoCancel(true)
.setGroup(GROUP_KEY_WORK_EMAIL)
.setSound(defaultSoundUri)
.setStyle(new NotificationCompat.BigTextStyle())
.setContentIntent(resultPendingIntent);
int mNotificationId = (int) System.currentTimeMillis();
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
}