使用FCM推送通知重定向无法正常工作

时间:2017-07-14 11:44:38

标签: android firebase firebase-cloud-messaging

我收到了来自服务器的多个通知。但问题只是最近通知重定向到应用程序。对于所有其他通知,单击通知时,应用程序未打开。这只发生在我的服务器上。如果我从FCM控制台发送多个通知,则所有通知都会重定向到应用程序。为什么这样开心呢?

下面是Json解析代码

     private void handleDataMessage(JSONObject json) {
                Log.e(TAG, "push json: " + json.toString());
                try {

                    JSONObject data = json.getJSONObject("data");

                    String title = data.getString("title");
                    String message = data.getString("message");
                    boolean isBackground = data.getBoolean("is_background");
                    String imageUrl = data.getString("image");
                    String timestamp = data.getString("timestamp");
                    String payload = data.getString("payload");

                    Log.e(TAG, "title: " + title);
                    Log.e(TAG, "message: " + message);
                    Log.e(TAG, "isBackground: " + isBackground);
                    Log.e(TAG, "payload: " + payload.toString());
                    Log.e(TAG, "imageUrl: " + imageUrl);
                    Log.e(TAG, "timestamp: " + timestamp);

                    if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
                        // app is in foreground, broadcast the push message
                        Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
                        pushNotification.putExtra("message", message);
                        pushNotification.putExtra("title", title);
                        LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

                        // play notification sound
                        NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
                        notificationUtils.playNotificationSound();
                    } else {
                        // app is in background, show the notification in notification tray
                        Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
                        resultIntent.putExtra("message", message);

                        // check for image attachment
                        if (TextUtils.isEmpty(imageUrl)) {
                            showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);
                        } else {
                            // image is present, show notification with image
                            showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, imageUrl);
                        }
                    }

                    sendNotification(title, message);
                } catch (JSONException e) {
                    Log.e(TAG, "Json Exception: " + e.getMessage());
                } catch (Exception e) {
                    Log.e(TAG, "Exception: " + e.getMessage());
                }

}

这是通知生成代码

private void sendNotification(String title, String message) {
            Intent intent = new Intent(MainMenuPage.this, ComplaintList.class);
            PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

            Notification n = new Notification.Builder(this)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setSmallIcon(R.drawable.icon)
                    .setContentIntent(pIntent)
                    .setAutoCancel(true)
                    .build();

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            notificationManager.notify(0, n);
        }

2 个答案:

答案 0 :(得分:1)

使用以下代码发送通知解决此问题

Notification n = new Notification.Builder(this)
            .setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.drawable.icon)
            .setContentIntent(pIntent)
            .setAutoCancel(true)
            .build();

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
    notificationManager.notify(m, n);

需要始终发送不同的通知ID。

答案 1 :(得分:0)

尝试以下代码对您有用:

 private void sendNotification(String message, String contentTypeId, String contentId) {

            if (notifManager == null) {
                notifManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
            }

            boolean isAppVisible = BaseActivity.count > 0;
            boolean isNotificationSound = true;
            //TODO: update count if app is open.
            if (isAppVisible) {
                if (isNotificationSound) {
                    try {
                        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
                        r.play();
                    } catch (Exception e) {
                    }
                }
                sendMessage(message, contentTypeId, contentId);
            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    int importance = NotificationManager.IMPORTANCE_HIGH;
                    assert notifManager != null;
                    NotificationChannel mChannel = notifManager.getNotificationChannel(CHANNEL_ID);
                    if (mChannel == null) {
                        mChannel = new NotificationChannel(CHANNEL_ID, getString(R.string.app_name), importance);
                        mChannel.enableVibration(true);
                        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                        notifManager.createNotificationChannel(mChannel);
                    }
                    builder = new NotificationCompat.Builder(this);
                    Intent intent = new Intent(this, SplashActivity.class);
                    intent.setAction(Intent.ACTION_MAIN);
                    //              intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    intent.putExtra(AppConstants.EXTRAS.IS_NOTIFICATION, true);
                    intent.putExtra(AppConstants.EXTRAS.CONTENT_ID, contentId);
                    pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
                    builder.setContentTitle(getString(R.string.app_name))                            // required
                            .setSmallIcon(getNotificationIcon())   // required
                            .setContentText(message).setWhen(System.currentTimeMillis()) // required
                            .setDefaults(Notification.DEFAULT_ALL)
                            .setAutoCancel(true)
                            .setContentIntent(pendingIntent)
                            .setChannelId(CHANNEL_ID)
                            .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                } else {
                    builder = new NotificationCompat.Builder(this);
                    Intent intent = new Intent(this, SplashActivity.class);
                    intent.setAction(Intent.ACTION_MAIN);
                    //              intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    intent.putExtra(AppConstants.EXTRAS.IS_NOTIFICATION, true);
                    intent.putExtra(AppConstants.EXTRAS.CONTENT_ID, contentId);
                    pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
                    builder.setContentTitle(getString(R.string.app_name))                            // required
                            .setSmallIcon(getNotificationIcon())   // required
                            .setContentText(message).setWhen(System.currentTimeMillis()) // required
                            .setDefaults(Notification.DEFAULT_ALL)
                            .setAutoCancel(true)
                            .setChannelId(CHANNEL_ID)
                            .setContentIntent(pendingIntent)
                            .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                            .setPriority(Notification.PRIORITY_HIGH);
                }
                Notification notification = builder.build();
                notifManager.notify(Integer.parseInt(contentId), notification);

            }
        }


        private void sendMessage(String message, String contentTypeId, String contentId) {

        }

        private static int getNotificationIcon() {
            return R.mipmap.ic_launcher;
        }
    }