【问题标题】:FCM App in background no notification sound and icon not displayedFCM App在后台没有通知声音和图标不显示
【发布时间】:2017-04-20 10:10:49
【问题描述】:

目前我发送通知为:

    {
        "data" : {
            "sound": "default",
            "icon": "ic_app_launcher",
            "..." : "..."
            .
            .
            .
         }
    }

E/fcmbundle:倾倒意图开始

E/fcmbundle:[google.sent_time=...]

E/fcmbundle:[来自=...]

E/fcmbundle:[icon=ic_app_launcher]

E/fcmbundle:[声音=默认]

E/fcmbundle:[google.message_id=0:...]

E/fcmbundle:[collapse_key=...]

E/fcmbundle:倾倒意图结束

这是当应用程序在后台运行时记录我从 FCM 获得的数据,点击通知时它将打开启动器 Activity。

我已经阅读了许多与同一问题相关的答案,但对我不起作用。我正在发送数据消息,并在其中收到了该消息。声音和图标也。但不工作。没有通知声音,图标显示为正方形。

I have also tried the single notification in json as:
{
    "to" : "...",
    "notification" : {
        "sound": "default",
        "icon" : "myicon"
    },
    "data" : {}
}

但这也不能正常工作。

通知在后台出现,但不发出声音,也不显示图标。

当应用程序处于前台时,通知会正常显示图标,并根据需要发出声音通知。

谁能帮我解决这个问题?

【问题讨论】:

    标签: android firebase notifications firebase-cloud-messaging


    【解决方案1】:

    对于前台和后台的通知,请考虑仅使用data 有效负载。然后override onMessageReceived 方法在你的custom MessageService class 下面是一个适合我的解决方案。 notificationPayload = {"data": { "title": "your title" ,"body": "Your body"}}

    public class CustomFirebaseMessagingService extends FirebaseMessagingService {
        final String CHANNEL_ID = "YOUR_CHANNEL_ID";//when targeting android-o
    
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        if (remoteMessage.getData().size() > 0) {
            sendNotification(remoteMessage);
        }
    }
    
    
    private void sendNotification(RemoteMessage remoteMessage) {
        Map<String, String> data = remoteMessage.getData();
    
        String title = data.get("title");
        String message = data.get("body");
    
        Intent resultIntent = new Intent(this, YOUR_TARGET_ACTIVITY.class);
    
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    
        PendingIntent resultPendingIntent = PendingIntent.getActivity(
                this,
                0,
                resultIntent,
                PendingIntent.FLAG_ONE_SHOT
        );
    
    
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setContentTitle(title)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(message))
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setAutoCancel(true);
    
    
        int notificationId = (int) System.currentTimeMillis();
        mBuilder.setSound(alarmSound);
        mBuilder.setContentIntent(resultPendingIntent);
    
    
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    
        if (manager != null) {
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                        "Human Readable channel id",
                        NotificationManager.IMPORTANCE_DEFAULT);
                channel.setShowBadge(true);
                manager.createNotificationChannel(channel);
    
            }
    
            manager.notify(notificationId, mBuilder.build());
        }
    }
    

    }

    【讨论】:

    • 我尝试了使用数据的解决方案,它现在有效。我需要做的就是正确处理数据部分。
    【解决方案2】:

    试试这个:

    @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
    
            if (remoteMessage != null && remoteMessage.getData().size() > 0) {
    
                try {
                    JSONObject json = new JSONObject(remoteMessage.getData().toString());
                    sendPushNotification(json);
                } catch (Exception e) {
                    Log.e(TAG, "Exception: " + e.getMessage());
                }
            }
          }
    

    sendPushNotification 方法

     private void sendPushNotification(JSONObject json) {
            try {
                //getting the json data
                JSONObject data = json.getJSONObject("data");
    
                   //creating MyNotificationManager object
                MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
    
                //creating an intent for the notification
    
                mNotificationManager.showSmallNotification(title, message, intent);
                OR
                mNotificationManager.showBigNotification(title, message, imageUrl, intent);
    
            } catch (JSONException e) {
                Log.e(TAG, "Json Exception: " + e.getMessage());
            } catch (Exception e) {
                Log.e(TAG, "Exception: " + e.getMessage());
            }
    }
    

    MyNotificationManager 类:

    public class MyNotificationManager {
    
        public int ID_BIG_NOTIFICATION = 234;
        public int ID_SMALL_NOTIFICATION = 235;
    
        private Context mCtx;
    
        public MyNotificationManager(Context mCtx) {
            this.mCtx = mCtx;
            ID_BIG_NOTIFICATION = getRandom();
            ID_SMALL_NOTIFICATION = getRandom();
        }
    
        private int getRandom() {
            Random random = new Random();
    
            return random.nextInt(99999) + 1;
        }
    
        //the method will show a big notification with an image
        //parameters are title for message title, message for message text, url of the big image and an intent that will open
        //when you will tap on the notification
        public void showBigNotification(String title, String message, String url, Intent intent) {
            PendingIntent resultPendingIntent =
                    PendingIntent.getActivity(
                            mCtx,
                            ID_BIG_NOTIFICATION,
                            intent,
                            PendingIntent.FLAG_UPDATE_CURRENT
                    );
            NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
            bigPictureStyle.setBigContentTitle(title);
            bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
            bigPictureStyle.bigPicture(getBitmapFromURL(url));
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx);
            Notification notification;
            notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
                    .setAutoCancel(true)
                    .setContentIntent(resultPendingIntent)
                    .setContentTitle(title)
                    .setStyle(bigPictureStyle)
                    .setDefaults(Notification.DEFAULT_VIBRATE|Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS)
                    .setSmallIcon(R.drawable.ic_app_icon)
                    .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.drawable.ic_app_icon))
                    .setContentText(message)
                    .build();
    
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
    
            NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(ID_BIG_NOTIFICATION, notification);
        }
    
        //the method will show a small notification
        //parameters are title for message title, message for message text and an intent that will open
        //when you will tap on the notification
        public void showSmallNotification(String title, String message, Intent intent) {
            PendingIntent resultPendingIntent =
                    PendingIntent.getActivity(
                            mCtx,
                            ID_SMALL_NOTIFICATION,
                            intent,
                            PendingIntent.FLAG_UPDATE_CURRENT
                    );
    
    
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx);
            Notification notification;
            notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
                    .setAutoCancel(true)
                    .setContentIntent(resultPendingIntent)
                    .setContentTitle(title)
                    .setSmallIcon(R.drawable.ic_app_icon)
                    .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.drawable.ic_app_icon))
                    .setContentText(message)
                    .build();
    
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
    
            NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(ID_SMALL_NOTIFICATION, notification);
        }
    
        //The method will return Bitmap from an image URL
        private Bitmap getBitmapFromURL(String strURL) {
            try {
                URL url = new URL(strURL);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                Bitmap myBitmap = BitmapFactory.decodeStream(input);
                return myBitmap;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    }
    

    【讨论】:

    • 谢谢,但实际上我试过了。但它似乎不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 2018-08-01
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    相关资源
    最近更新 更多