【问题标题】:Handling FCM Notification while running and killed在运行和终止时处理 FCM 通知
【发布时间】:2017-11-14 07:45:19
【问题描述】:

我正在寻找一种在应用程序运行、后台或被终止时处理 fcm 通知的模式。

我们的应用程序具有登录过程,因此当应用程序未启动时,通知需要打开启动器,但是当应用程序运行时,我只想在用户单击通知时推送一个活动。

所以我猜最好的方法是 ContentIntent,它要么被传递到堆栈中最顶层的 Activity,要么被传递到 Launch-Activity。

无论应用程序是否正在运行,我当前的代码总是使用 TaskStackBuilder 重新启动应用程序。

我的代码示例:

resultIntent = new Intent(this, MyProfileActivity.class);
resultIntent.putExtra(ActivityUtil.KEY_IMAGE_URL, SharedPref.getUserImage(this));
resultIntent.putExtra(KEY_NOTIFICATION_ID, notificationID);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(SetDetailReworked.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "...")
            .setLargeIcon(getBitmapFromUrl(images[UI.GetDensityInt(this)]))
            .setSmallIcon(R.drawable.bar_icon)
            .setContentTitle(getString(R.string.push_notification_Title))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(resultPendingIntent);

Notification not = builder.build();

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

notificationManager.notify(ID, not);

【问题讨论】:

    标签: android firebase android-intent push-notification firebase-cloud-messaging


    【解决方案1】:

    尝试在androidManifest中将你的MyProfileActivity的launchMode设置为singleInstance,

            <activity
            android:name=".MyProfileActivity"
            android:label="@string/app_name"
            android:launchMode="singleInstance"
    

    当用户单击通知时,它不会重新启动您的后台活动。相反,MyProfileActivity 的 onNewIntent 将被触发。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多