【问题标题】:Multiple Push notifications in androidandroid中的多个推送通知
【发布时间】: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);
            }
        }

【问题讨论】:

    标签: android push-notification android-push-notification


    【解决方案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);
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多