简体   繁体   English

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

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

So i am experiencing a strange behavior in firebase messaging. 所以我在Firebase消息传递中遇到一种奇怪的行为。 I am sending both [notification] and [data] objects in my firebase messaging FCM call, when the app is in background, i can receive push notification as many as i call the FCM request. 我在Firebase消息传递FCM调用中同时发送[notification]和[data]对象,当应用程序在后台运行时,我可以收到与调用FCM请求一样多的推送通知。 but the issue now when i receive the push notification while opening the app, if i clicked the notification i will no longer receive any further push notifications. 但是现在的问题是,当我在打开应用程序时收到推送通知时,如果单击通知,我将不再收到任何进一步的推送通知。

NOTE: If i forced stop the then reopened again, i will start receiving notification again. 注意:如果我强行停止,然后再次重新打开,则我将再次开始接收通知。

I tested this issue on several android devices with several android versions and getting the same results. 我在具有多个Android版本的多个Android设备上测试了此问题,并获得了相同的结果。

Btw, i am using compile 'com.google.firebase:firebase-messaging:11.4.0' . 顺便说一句,我正在使用compile 'com.google.firebase:firebase-messaging:11.4.0'

UPDATE: here is the code 更新:这是代码

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;
    }
} 

I found what causing the issue. 我找到了导致问题的原因。 for some reason 由于某些原因

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

and

Looper.loop();

Shouldn't be in the code and was preventing onMessageReceived to be called again. 不应该在代码中,并且阻止了onMessageReceived被再次调用。

暂无
暂无

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

相关问题 Firebase 云消息 - 手机收不到推送通知 - Firebase Cloud Messaging - Mobile not receiving push notification 当应用程序不在前台时,如何使用Firebase Cloud Messaging(Notification)在Android中获取抬头通知? - How to get Heads Up notification in android using Firebase Cloud Messaging(Notification) while the app is not in foreground? Firebase 云消息推送通知无法在前台工作,仅在后台工作,在 Android - Firebase Cloud Messaging push notification not working in foreground, only background, on Android 当应用程序处于前台状态时接收通知-(第28页) - Receiving notification while app is in foreground - (pie 28) Android:Firebase 应用程序在前台时推送通知不同的图标,而当应用程序在后台时,另一个图标 - Android: Firebase push notification different icon while app is on foreground and another while app is on background 接收GCM推送通知(Cordova)时将应用程序带到前台 - Bring app to foreground when receiving GCM Push notification (Cordova) 在Android中-当应用程序处于前台时如何防止Firebase推送通知? - In android - How to prevent Firebase push notification when app is in foreground? 应用程序在前台时推送通知(使用 Firebase 触发器) - Push notification when app is in foreground (using Firebase triggers) 前台应用程序抖动时未收到 Firebase 推送通知? - Firebase Push Notification not received when flutter app in foreground? Flutter:前台通知在 Firebase 消息传递中不起作用 - Flutter: Foreground notification is not working in firebase messaging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM