根据主题发送fcm通知

时间:2017-05-05 08:37:06

标签: android firebase push-notification

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

1 个答案:

答案 0 :(得分:0)

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