【问题标题】:in the background execute cannot back to correct Activity page在后台执行无法返回正确的活动页面
【发布时间】: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


    【解决方案1】:

    更改通知代码就好了。

    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());   }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    相关资源
    最近更新 更多