如何通过FCM接收时自定义显示的通知?

问题描述 投票:0回答:1

当我从后端发送通知时,我同时接收到notification有效负载和data有效负载。现在,当从后端触发通知时,会自动接收默认通知。

此(默认)即将到来的通知是否可以被覆盖(如UI更改或未决的意图更改)?

[我尝试同时使用notification有效负载和data有效负载(后来我知道当应用程序为后台时通知有效负载不起作用),但最终没有任何帮助。

作为最后的选择,我正在考虑不发送任何notification有效负载数据,而仅发送data有效负载中的必需通知数据-这样可以吗?

为此附加代码段。(我最后一次尝试使用datapayload一个值)

 public class AppFcmReceiverData extends FirebaseMessagingService {
        private static final String CHANNEL_ID = "ds";
        private static final String TAG = "4";
        private int numMessages = 0;
        private Context mContext;
        private String newsid;
        private NotificationManagerCompat notificationManager;
        private NotificationUtils notificationUtils;
        /*
            private ArrayList<NewsData.notificationData> notificationDatalist;
        */
        NewsData newsData;
        private Map<String, String> notificationdata;

        @Override
        public void onMessageReceived(@NotNull RemoteMessage remoteMessage) {
            mContext=AppFcmReceiverData.this;

            if (remoteMessage == null)
                return;

            if (remoteMessage.getNotification() != null) {
                Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody().toString());
                handleNotification(remoteMessage.getNotification().getBody());
            }

            if (remoteMessage.getData().size() > 0) {
                Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

                try {
                    JSONObject json = new JSONObject(remoteMessage.getData().toString());
                    handleDataMessage(json);
                } catch (Exception e) {
                    Log.e(TAG, "Exception: " + e.getMessage());
                }
            }

        }

        private void handleNotification(String body) {
                // app is in foreground, broadcast the push message
                Intent pushNotification = new Intent(Constants.PUSH_NOTIFICATION);
                pushNotification.putExtra("message", body);
                LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);


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

    ...................................................................



          public class NotificationUtils {
            private static String TAG = NotificationUtils.class.getSimpleName();

            private Context mContext;

            public NotificationUtils(Context mContext) {
                this.mContext = mContext;
            }

            public void showNotificationMessage(String title, Intent intent1, Intent intent) {
                showNotificationMessage(title,intent,null);
            }

            public void showNotificationMessage(final String title,Intent intent) {
                if (TextUtils.isEmpty(title))
                    return;


                // notification icon
                final int icon = R.mipmap.ic_launcher;

                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                final PendingIntent resultPendingIntent =
                        PendingIntent.getActivity(
                                mContext,
                                0,
                                intent,
                                PendingIntent.FLAG_CANCEL_CURRENT
                        );

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

                final Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                        + "://" + mContext.getPackageName() + "/raw/notification");
                showSmallNotification(mBuilder, icon, title/*, message, timeStamp,*/ ,resultPendingIntent, alarmSound);

            }


            private void showSmallNotification(NotificationCompat.Builder mBuilder, int icon, String title,/* String message, String timeStamp, */PendingIntent resultPendingIntent, Uri alarmSound) {

                NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

              //  inboxStyle.addLine(message);

                Notification notification;
                notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
                        .setAutoCancel(true)
                        .setContentTitle(title)
                        .setContentIntent(resultPendingIntent)
                        .setSound(alarmSound)
                        .setStyle(inboxStyle)
                        //.setWhen(getTimeMilliSec(timeStamp))
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
                       // .setContentText(message)
                        .build();

                NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.notify(Integer.parseInt(String.valueOf(Constants.NOTIFICATION_ID)), notification);
            }
android firebase-cloud-messaging firebase-notifications
1个回答
0
投票

是的,有可能。从控制台/管理员sdk发送FCM时,您可以省略notification有效负载,而仅填充data有效负载。

您可以在android应用的服务上覆盖onMessageReceived(RemoteMessage remoteMessage),您可以在其中接收并解析data有效负载,并使用自定义的待处理意图构建自定义通知。请参见示例here

您可以找到有关如何在android设备here上构建和显示自定义通知的详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.