在后台应用程序时,数据从FCM接收,但不显示通知

时间:2018-07-23 08:49:07

标签: android firebase push-notification notifications firebase-cloud-messaging

我有一个从FCM接收通知的Android应用。我的应用程序在后台和前台都具有通知有效负载。我尝试将通知有效负载更改为数据有效负载。数据在我的应用程序中接收并在前台状态下正确运行,但是当我的应用程序在后台时,onReceiveMessage方法调用并运行,但未在通知栏中向用户显示通知!我从服务器获取数据并转换为json,但无法显示通知。

 @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
 if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

        try {
            Map<String,String> params=remoteMessage.getData();
            JSONObject json=new JSONObject(params);
            //JSONObject json = new JSONObject(remoteMessage.getData().toString());
            handleDataMessage(json);
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }
}
private void handleDataMessage(JSONObject json) {
    Log.e(TAG, "push json: " + json.toString());

    try {
        String title = json.getString("ticket");
        String message = json.getString("body");

        if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            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);
            //
            resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            final PendingIntent resultPendingIntent =
                    PendingIntent.getActivity(
                            this,
                            0,
                            resultIntent,
                            PendingIntent.FLAG_ONE_SHOT
                    );

            final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    this);

            final Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                    + "://" + this.getPackageName() + "/raw/notification");

            mBuilder.setSmallIcon(R.drawable.ic_launcher)
                    .setAutoCancel(true)
                    .setContentTitle(title)
                    .setContentIntent(resultPendingIntent)
                    .setSound(alarmSound)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentText(message);

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(Config.NOTIFICATION_ID, mBuilder.build());

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

当我编译我的应用程序时,我的应用程序运行良好并调用handeDataMessage但未显示通知!

0 个答案:

没有答案