在应用程序处于前台时收到推送通知后,Firebase消息将停止

时间:2017-10-31 21:34:02

标签: android firebase firebase-cloud-messaging

所以我在firebase消息传递中遇到了一个奇怪的行为。我在我的firebase消息传递FCM调用中发送[notification]和[data]对象,当应用程序在后台时,我可以接收推送通知,就像我调用FCM请求一样多。但问题是,当我在打开应用程序时收到推送通知时,如果我点击通知我将不再收到任何进一步的推送通知。

注意:如果我强制停止然后再次重新打开,我将再次开始接收通知。

我在几个具有多个Android版本的Android设备上测试了这个问题并得到了相同的结果。

是的,我正在使用compile 'com.google.firebase:firebase-messaging:11.4.0'

更新:这是代码

public class GcmMessageHandler extends FirebaseMessagingService {
    public static final int MESSAGE_NOTIFICATION_ID = 435345;
    String title, Type, Link, AppVersion, discountExpiry, discountValue, status,
            phone, nid;
    PreferencesHelper ph;
    Map data;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        if (Looper.myLooper() == null) {
            Looper.prepare();
        }

        ph = new PreferencesHelper(getApplicationContext());

        data = remoteMessage.getData();
        String click_action = remoteMessage.getNotification().getClickAction();
        Intent intentNotifi = new Intent(click_action);
        intentNotifi.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        title = (String) data.get("title");
        Type = (String) data.get("type");
        Link = (String) data.get("link");
        discountExpiry = (String) data.get("discountexpiry");
        discountValue = (String) data.get("discvalue");
        AppVersion = (String) data.get("appversion");
        status = (String) data.get("status");
        phone = (String) data.get("delivery_phone");
        nid = (String) data.get("nid");

        if (Type != null) {
            Intent notificationIntent;
            switch (Type) {
                case "simple_notification":
                    notificationIntent = new Intent(getApplicationContext(), SplashActivity.class);
                    show_notification(title, notificationIntent);
                    break;

                case "order_status":
                    notificationIntent = new Intent(getApplicationContext(), OrderActivity.class);
                    notificationIntent.putExtra("fromNotification", true);
                    show_notification(title, notificationIntent);
                    break;

                case "news":
                    notificationIntent = new Intent(getApplicationContext(), DetailsFromNotification.class);
                    notificationIntent.putExtra("nidRef", nid);
                    notificationIntent.getBooleanExtra("fromNotification", true);
                    show_notification(title, notificationIntent);
                    break;

                case "link":
                    notificationIntent = new Intent(Intent.ACTION_VIEW);
                    notificationIntent.setData(Uri.parse(Link));
                    show_notification(title, notificationIntent);
                    break;

                case "deal_notify":
                    notificationIntent = new Intent(getApplicationContext(), DealsActivity.class);
                    notificationIntent.putExtra("fromNotification", true);
                    show_notification(title, notificationIntent);
                    break;

                case "after_ordering":
                    notificationIntent = new Intent(getApplicationContext(), OrderActivity.class);
                    notificationIntent.putExtra("fromNotification", true);
                    show_notification(title, notificationIntent);
                    break;

                case "no_orders":
                    notificationIntent = new Intent(getApplicationContext(), SplashActivity.class);
                    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    show_notification(title, notificationIntent);
                    break;

                case "no_signup":
                    notificationIntent = new Intent(getApplicationContext(), UserActivity.class);
                    notificationIntent.putExtra("fromNotification", true);
                    show_notification(title, notificationIntent);
                    break;

                case "user_points":
                    notificationIntent = new Intent(getApplicationContext(), PointsActivity.class);
                    notificationIntent.putExtra("fromNotification", true);
                    show_notification(title, notificationIntent);
                    break;
                default:
                    notificationIntent = new Intent(getApplicationContext(), SplashActivity.class);
                    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    show_notification(title, notificationIntent);
                    break;
            }
        }

        Looper.loop();
    }


    private void show_notification(String title, Intent intent) {
        Context context = getBaseContext();
        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(getNotificationIcon()).setContentText(title).
                        setContentIntent(contentIntent)
                .setAutoCancel(true).setDefaults(Notification.DEFAULT_SOUND |
                        Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
    }

    private int getNotificationIcon() {
        boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >=
                android.os.Build.VERSION_CODES.LOLLIPOP);
        return useWhiteIcon ? R.drawable.logo_notification :
                R.drawable.logo_notification;
    }
} 

1 个答案:

答案 0 :(得分:0)

我找到了造成这个问题的原因。由于某种原因

 if (Looper.myLooper() == null) {
            Looper.prepare();
        }

Looper.loop();

不应该在代码中,并且阻止再次调用onMessageReceived。