【发布时间】:2020-11-09 09:30:22
【问题描述】:
在后台执行无法返回正确的活动页面。
我有fid值,如果我在应用程序中收到通知然后我单击通知,我可以转到正确的活动页面,但是当我在后台收到通知时,当我单击通知时执行,它是总是去 MainActivity。
哪里有问题?
这是代码
public class MyFirebaseService extends FirebaseMessagingService {
SessionManager sessionManager;
private String getID,fid;
private Intent intent;
@Override
public void onCreate() {
super.onCreate();
sessionManager = new SessionManager(this);
sessionManager.checkLogin();
HashMap<String, String> user = sessionManager.getUserDetail();
getID = user.get(sessionManager.USERID);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getNotification() != null) {
Log.i("MyFirebaseService","title "+remoteMessage.getNotification().getTitle());
Log.i("MyFirebaseService","body "+remoteMessage.getNotification().getBody());
Map<String, String> data = remoteMessage.getData();
if(data.get("fid") != null){
fid = data.get("fid").toString();
}
sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), fid);
}
}
private void sendNotification(String messageTitle,String messageBody, String fid) {
if(fid != null){
intent = new Intent(this, NewNotificationPostInfoActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, fid);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}else{
intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = "channelID";
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.luvtas_au2);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setContentTitle(messageTitle)
.setSmallIcon(R.drawable.luvtas_au3)
.setLargeIcon(largeIcon)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_ALL)
.setGroup(channelId)
.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());
}
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.i("MyFirebaseService","token "+s);
}
}
【问题讨论】:
标签: android push-notification notifications firebase-cloud-messaging android-notifications