在后台执行无法返回到正确的"活动"页面



在后台执行无法返回到正确的"活动"页面。

我有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);
}
}

像这样更改通知代码。

private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
long notificatioId = System.currentTimeMillis();
Intent intent = new Intent(getApplicationContext(), TestActivity.class); // Here pass your activity where you want to redirect.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), intent, 0);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
currentapiVersion = R.mipmap.ic_notification_lolipop;
} else{
currentapiVersion = R.mipmap.ic_launcher;
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(currentapiVersion)
.setContentTitle(this.getResources().getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent);
mNotificationManager.notify((int) notificatioId, notificationBuilder.build());   }

相关内容

  • 没有找到相关文章

最新更新