PendingIntent 不适用于 FCM 通知

PendingIntent not working with FCM notification

我想在通知点击事件中打开详细信息 activity。 这里是PendingIntentNotification集合class。我加了addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)也加了PendingIntent.FLAG_UPDATE_CURRENT。我在网站上搜索了所有主题,我找到了这个解决方案,我尝试了所有这些解决方案。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";
    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO(developer): 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. See sendNotification method below.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

        ArrayList<String> notificationData = new ArrayList<String >(remoteMessage.getData().values());
        String fortuneID = notificationData.get(0);
        sendNotification(remoteMessage.getNotification().getBody(),fortuneID);
    }
    // [END receive_message]

    /**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param messageBody FCM message body received.
     */
    private void sendNotification(String messageBody,String notificationID) {

        Intent configureIntent = new Intent(getApplicationContext(), FalDetayActivity.class);
        configureIntent.putExtra("extra", "123123");
        configureIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        configureIntent.setAction("dummy_unique_action_identifyer" + "123123");
        int dummyuniqueInt = new Random().nextInt(543254);
        PendingIntent pendingClearScreenIntent = PendingIntent.getBroadcast(getApplicationContext(), dummyuniqueInt, configureIntent, PendingIntent.FLAG_UPDATE_CURRENT);

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

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("myApp")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingClearScreenIntent);

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

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

这是 getBundle 部分 MainActivity

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String quote = getIntent().getStringExtra("extra");

        ...
}

另一个问题我想打开FalDetailActivity但总是打开MainActiviygetIntent()总是return空。可能是我的 PendingIntent 未设置 我没有发现错误 请检查此代码。谢谢...

更新

我将 getBroadcast 更新为 getActivity 并删除了一些内容

        Intent configureIntent = new Intent(getApplicationContext(), FalDetayActivity.class);
        configureIntent.putExtra("extra", "123123");
        configureIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingClearScreenIntent = PendingIntent.getActivity(getApplicationContext(), 0, configureIntent, PendingIntent.FLAG_UPDATE_CURRENT);

您想启动一个带有待定意图的 activity,而不是发送广播。 将 PendingIntent.getBroadcast(...) 更改为 PendingIntent.getActivity(...).

尝试使用服务的上下文,而不是使用 getApplicationContext(),即将 getApplicationContext() 替换为 this

由于您是从服务的上下文中启动 activity,因此您还需要添加新的任务意图标志 -

configureIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

此外,将这些标志用于您的未决意图 -

PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT

最后我找到了解决方案: https://firebase.google.com/docs/notifications/android/console-audience

当您的应用程序在后台运行时,Android 会将通知消息定向到系统托盘。用户点击通知会默认打开应用程序启动器。

这包括包含通知和数据负载的消息。在这些情况下,通知会传送到设备的系统托盘,并且数据有效负载会在您的启动器意图的额外内容中传送 Activity。