【问题标题】:Sending fcm Notifications based on topics根据主题发送 fcm 通知
【发布时间】:2017-05-05 08:37:05
【问题描述】:

当我们根据主题发送FCM通知时,发送通知后是否会到达订阅主题的设备?

【问题讨论】:

    标签: android firebase push-notification


    【解决方案1】:
    In FCM Notification we are getting a message on RemoteMessage.
    if you want to get a body then call remoteMessage.getNotification().getBody(). 
    
    //This method is only generating push notification
        //It is same as we did in earlier posts
        private void sendNotification(RemoteMessage remoteMessage) {
            Intent resultIntent = new Intent(this, MenuBarActivity.class);
            resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            HashMap<String, String> dataMap = new HashMap<String, String>(remoteMessage.getData());
            resultIntent.putExtra(Constant.KEY_MESSAGE_BODY, dataMap);
            resultIntent.putExtra(Constant.KEY_IS_NOTIFICATION, true);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent,
                    PendingIntent.FLAG_ONE_SHOT);
    
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(getString(R.string.app_name))
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getData().get(Constant.KEY_BODY)))
                    .setContentText(remoteMessage.getData().get(Constant.KEY_BODY))
                    .setAutoCancel(true)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                    .setSound(defaultSoundUri);
    
    //        if (!BaseActivity.isInForeground()) {
            notificationBuilder.setContentIntent(pendingIntent);
    //        }
            // Generate ID
            long time = new Date().getTime();
            String tmpStr = String.valueOf(time);
            String last4Str = tmpStr.substring(tmpStr.length() - 5);
            int notificationId = Integer.valueOf(last4Str);
    
            // Sets an ID for the notification
    //            int notificationId = Constant.NOTIFICATION_ID;
    
            // Gets an instance of the NotificationManager service
            NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    
            // Builds the notification and issues it.
            mNotifyMgr.notify(notificationId, notificationBuilder.build());
        }
    

    【讨论】:

      猜你喜欢
      • 2019-11-06
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 2018-03-22
      • 2019-07-19
      • 2021-01-12
      相关资源
      最近更新 更多