单击通知时出现错误的活动(PendingIntent)

时间:2018-05-28 11:07:32

标签: java android firebase service firebase-cloud-messaging

我在我的Android应用程序(FirebaseMessagingService)中实现了通知服务。单击通知并关闭应用程序时,将打开错误的活动。如果在应用程序打开(前景,背景)时通知到达,则打开的活动是正确的。

在以下代码中,将打开启动活动(项目的第一个活动)而不是IntroNoti.class。 这是我的FireBaseMessagingService类的代码

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO: Handle FCM messages here.
    // If the application is in the foreground handle both data and notification messages here.
    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated.
    //Log.d(TAG, "From: " + remoteMessage.getFrom());
    //Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    Globals.messageIn = true;
    sendNotification(remoteMessage);
}

private void sendNotification(RemoteMessage remoteMessage) {

    //Intent intent = new Intent(this, Login.class);
    Intent intent = new Intent(this, IntroNoti.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    int icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.ic_launcher: R.mipmap.ic_launcher;
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(icon)
            .setContentTitle(remoteMessage.getNotification().getTitle())
            .setContentText(remoteMessage.getNotification().getBody())
            .setContentText("CIAO CIAO")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

2 个答案:

答案 0 :(得分:0)

感谢@Eugen Pechanec的建议,我在MainActivity中添加了这条指令。只有在通知打开应用程序时才会评估“messageid”属性。 谢谢你们!

if (getIntent() != null && getIntent().getExtras() != null && getIntent().getExtras().size() > 0) {
        Log.d(TAG, "Received extras in onCreate()");
        Bundle extras = getIntent().getExtras();
        if (!extras.getString("messageid","").isEmpty()) {
            Globals.fromNotification = true;
        }

    }

答案 1 :(得分:0)

PendingIntent.FLAG_UPDATE_CURRENT 标志与待处理的意图一起使用。这对我有用。

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplication(), CHANNEL_ID)
                .setSmallIcon(R.drawable.logo)
                .setContentTitle(title)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_CALL)
                .setContentIntent(getPendingIntent())
                .setAutoCancel(true);

private PendingIntent getPendingIntent() {
    Intent intent = new Intent(getApplication(), ConversationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    return PendingIntent.getActivity(getApplication(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}