问题
I am trying to send notifications to devices when they receive messages from their friends. But if they are about to receive the pop up, the app automatically goes to the homepage before the message even shows. I just don't get it. The messages are both data and notification types. I just can't tell where the error is from. Please help. Below is my code.
JS code.
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);
});
});
});
My 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());
}
}
来源:https://stackoverflow.com/questions/51228271/app-malfunctioning-when-firebase-notification-pops-up