android中的多个推送通知

Posted

技术标签:

【中文标题】android中的多个推送通知【英文标题】:Multiple Push notifications in android 【发布时间】:2017-07-16 13:16:57 【问题描述】:

我正在开发接收推送通知的 android 应用程序。我也在不同的论坛上搜索了很多答案,但没有找到适合这个问题的解决方案。我什至尝试更改Notification ID,以免获得多次推送。我用过Intent.FLAG_ACTIVITY_CLEAR_TOP,但效果不佳。

我的代码:

public void showNotificationMessage(String title, String message, Intent intent) 

            // Check for empty push message
            if (TextUtils.isEmpty(message))
                return;

            if (isAppIsInBackground(mContext)) 
                // notification icon
                int icon = R.mipmap.ic_logo;



                PendingIntent resultPendingIntent =
                        PendingIntent.getActivity(
                                mContext,
                                0,
                                intent,
                                PendingIntent.FLAG_CANCEL_CURRENT
                        );

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

                PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                        mContext);
                Notification notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
                        .setAutoCancel(true)
                        .setContentTitle(title)
                        .setStyle(inboxStyle)
                        .setContentIntent(resultPendingIntent)
                        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                        .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
                        .setContentText(message)
                        .build();

                mBuilder.setContentIntent(contentIntent);

                NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.notify(count++, notification);
             else 
                intent.putExtra("title", title);
                intent.putExtra("message", message);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                mContext.startActivity(intent);
            
        

【问题讨论】:

【参考方案1】:
NOTIFICATION_ID++ no count++





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

        Notification notification;

        if (iconBitMap == null) 
            //When Inbox Style is applied, user can expand the notification

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

            inboxStyle.addLine(message);

            notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
                    .setAutoCancel(true)
                    .setContentTitle(title)
                    .setContentIntent(resultPendingIntent)
                    .setStyle(inboxStyle)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(iconBitMap)
                    .setWhen(System.currentTimeMillis())

                    .setContentText(message)
                    .build();

         else 
            //If Bitmap is created from URL, show big icon
            NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
            bigPictureStyle.setBigContentTitle(title);
            bigPictureStyle.setSummaryText(html.fromHtml(message).toString());
            bigPictureStyle.bigPicture(iconBitMap);
            notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
                    .setAutoCancel(true)
                    .setContentTitle(title)
                    .setContentIntent(resultPendingIntent)
                    .setStyle(bigPictureStyle)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(iconBitMap)
                    .setContentText(message)
                    .setBadgeIconType( 4 )
                    .setWhen(System.currentTimeMillis())

                    .build();
        

        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(NOTIFICATION_ID++, notification);

    

【讨论】:

以上是关于android中的多个推送通知的主要内容,如果未能解决你的问题,请参考以下文章

在Android中处理多个推送通知点击

Parse -Android 获取多个推送通知

Android 通知 - 为多个用户推送通知

带有队列的laravel中的android和ios推送通知

向多个用户发送推送通知android

如何在 Android 中禁用多个推送通知图标,我正在使用 Parse.com