繁体   English   中英

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

[英]Firebase messaging stops after receiving a push notification while app in foreground

所以我在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;
    }
} 

我找到了导致问题的原因。 由于某些原因

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

Looper.loop();

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM