处理连续的fcm推送通知

时间:2017-07-20 20:00:33

标签: android android-intent notifications

我已经设置了自己的推送通知服务器来向Android设备发送通知。

我已经处理了通知,所以点击它总是打开我的自定义活动,无论应用程序是在后台还是前台。

工作部分:当发送两个分开的通知时,意味着收到第一个通知时,我点击它以便启动活动即可。 我重新发送另一个通知,所以当我点击它一切顺利,活动重新启动。完美!

NOT工作部分:发送两个连续通知时,会出现问题。 当收到两个通知时..i打开第一个活动启动但是当我点击第二个时没有任何事情发生!!。

所以我认为这可能是改变Intent FLAG或Pending Intent的解决方案。

我已经搜索了解决方案,但所有这些都是关于在app处于前台或后台时处理通知,而不是我的情况。

这是我的工作代码:

Intent i = new Intent(this, News_description.class);
                    i.putExtra("title", title);
                    i.putExtra("message", message);
                    i.putExtra("image", image);
                    i.putExtra("time", time);
                    i.putExtra("date", date);
                    i.putExtra("click_action", click_action);
                    i.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i,PendingIntent.FLAG_ONE_SHOT
                    );
                    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

                    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setVibrate(new long[]{status, status})
                            .setContentTitle(getString(R.string.app_name))
                            .setContentText(title)
                            .setStyle(new NotificationCompat.BigTextStyle().bigText(title))
                            .setSmallIcon(R.mipmap.ahed_me)
                            .setContentIntent(pendingIntent);
                    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    int id = (int) System.currentTimeMillis();
                    manager.notify(id, builder.build());
                } catch (Exception e) {
                    e.printStackTrace();
                }

我可以提供任何进一步的信息。

2 个答案:

答案 0 :(得分:0)

我有Flag FLAG_ACTIVITY_CLEAR_TASK,它适用于我的应用。

i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

这是我的完整代码,对我来说非常适合:

       Intent i = new Intent(getApplicationContext(), MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent contentIntent =
                PendingIntent.getActivity(getApplicationContext(), 0,
                        i, 0);

        NotificationCompat.Builder mBuilder =
                (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
                        .setSmallIcon(R.drawable.alarm)
                        .setAutoCancel(true)
                        .setContentTitle(title)
                        .setPriority(Notification.PRIORITY_MAX)
                        .setVibrate(vibrate)
                        .setLights(Color.BLUE, 3000, 1500)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(body))
                        .setContentIntent(contentIntent)

                        .setContentText(body);


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

        int Unique_Integer_Numbe =  (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
        mNotificationManager.notify(Unique_Integer_Numbe, mBuilder.build());

答案 1 :(得分:0)

<强>更新:

public SomePushNotificationClass {

    private  static int NOTIFICATION_ID = 1;
    String previousMessageID = "null";

    @Override
    public void onReceive(Bundle data) {

        String messageID = data.getString("message_id");

        if (!messageID.equals(previousMessageID) && !messageID.isEmpty()) {
            previousMessageID = messageID;

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

            Intent it = new Intent(this, SomeActivity.class);
            it.putExtra("key_example_1", someValue);
            it.putExtra("key_example_2", someValue2);
            it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
               | Intent.FLAG_ACTIVITY_SINGLE_TOP);

            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, it, PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
            notificationBuilder.setSmallIcon(R.drawable.some_drawable);
            notificationBuilder.setLargeIcon(someIcon);
            notificationBuilder.setContentTitle(someTitle);
            notificationBuilder.setStyle(new 

            // Optional
            NotificationCompat.BigTextStyle().bigText(someText));
            notificationBuilder.setContentText(someText);
            notificationBuilder.setAutoCancel(true);

            notificationBuilder.setContentIntent(pendingIntent);
            notificationManager.notify(NOTIFICATION_ID, 
            notificationBuilder.build());

            NOTIFICATION_ID++;
       }
   }
}

这就是你需要的:

 Intent it = new Intent(this, SomeActivity.class);
        it.putExtra("key_example_1", someValue);
        it.putExtra("key_example_2", someValue2);
        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);