在后台单击通知无法正常工作时,使用FCM打开特定活动

时间:2019-07-15 10:50:36

标签: android notifications firebase-cloud-messaging

我正在使用FCM推送通知。点击通知时,我正在传递启动新活动的意图。当应用程序处于前台状态时,应用程序可以正常运行,并且意图启动新活动,但是当应用程序处于后台时,它不会启动新活动,而是启动默认活动的实例。

我使用了2个功能,当我从前台收到通知时使用第一个功能,而当我从后台收到通知时使用第二个功能。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        if (remoteMessage.getData().isEmpty())

// when app is in foreground
            ShowNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());

        else

// when app is in background

            ShowNotification(remoteMessage.getData());

    }

后台使用的功能:

    public void ShowNotification(Map<String, String> data) {

        String title = data.get("title").toString();
        Log.d(TAG, "Title : " +title);
        String body = data.get("body").toString();
        Log.d(TAG, "Body : " + body);

        String currentDateTimeString = DateFormat.getDateInstance().format(new Date());

        Intent notificationIntent = new Intent(this, NotificationActivity.class);
/*
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
*/

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        notificationIntent.putExtra("title", title);
        notificationIntent.putExtra("body", body);
        notificationIntent.putExtra("date", currentDateTimeString);
        notificationIntent.setAction(title);
        notificationIntent.setAction(body);
        notificationIntent.setAction(currentDateTimeString);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "com.example.firebasecloudmessaging.test";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification",
                    NotificationManager.IMPORTANCE_DEFAULT);

            notificationChannel.setDescription("Hamza");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.setVibrationPattern(new long[]{0,1000,500,1000});
            notificationChannel.enableLights(true);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
        notificationBuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(title)
                .setContentText(body)
                .setContentIntent(pendingIntent)
                .setContentInfo("Infos");

        notificationManager.notify(new Random().nextInt(),notificationBuilder.build());

    }

0 个答案:

没有答案