如何在android中显示多个通知

Kar*_*k s 93 notifications android android-notifications

我只收到一个通知,如果有另一个通知,它将替换前一个通知,这是我的代码

private static void generateNotification(Context context, String message,
        String key) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context,
            FragmentOpenActivity.class);
    notificationIntent.putExtra(key, key);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notification.defaults |= Notification.DEFAULT_SOUND;

    // notification.sound = Uri.parse("android.resource://" +
    // context.getPackageName() + "your_sound_file_name.mp3");
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);

}
Run Code Online (Sandbox Code Playgroud)

San*_*hah 121

只需用这个替换你的线

 notificationManager.notify(Unique_Integer_Number, notification);
Run Code Online (Sandbox Code Playgroud)

希望它会对你有所帮助.

  • 生成唯一整数:(int)((new Date().getTime()/ 1000L)%Integer.MAX_VALUE); (19认同)
  • 唯一整数表示您必须设置永不重复的整数值.例子0,1,2,3,4,5,.... !!!! (4认同)
  • 什么是代码中的"Unique_Integer_Number"..以及它应该替换的代码 (2认同)
  • NotificationManager.notify(1, 通知); NotificationManager.notify(2, 通知); (2认同)

sag*_*oid 75

简单的notification_id需要是可更改的.

只需为notification_id创建随机数.

    Random random = new Random();
    int m = random.nextInt(9999 - 1000) + 1000;
Run Code Online (Sandbox Code Playgroud)

或者您可以使用此方法创建tieorange告知的随机数(这将永远不会重复):

    int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
Run Code Online (Sandbox Code Playgroud)

并替换此行以添加通知ID的参数以生成随机数

    notificationManager.notify(m, notification);
Run Code Online (Sandbox Code Playgroud)

  • 有点hacky并且可能会最终得到相同的通知ID,但如果您需要非常快速的东西,这可行. (8认同)
  • 如果我看对了,那么来自 tieorange 的方法只适用于几秒钟。因此,如果您同时有多个通知,这将不起作用。 (2认同)
  • @testing 是对的。这就是为什么我有第二步,m += random.nextInt(100) + 1; 这可能是额外的一步,但它的方式更安全。我看到上述方法在拍卖/出价应用程序的最后几分钟失败了。因此,为了安全起见,我添加了另一行! (2认同)

vLo*_*pez 27

使用共享首选项对我有用

SharedPreferences prefs = getSharedPreferences(Activity.class.getSimpleName(), Context.MODE_PRIVATE);
int notificationNumber = prefs.getInt("notificationNumber", 0);
...

notificationManager.notify(notificationNumber , notification);
SharedPreferences.Editor editor = prefs.edit();
notificationNumber++;
editor.putInt("notificationNumber", notificationNumber);
editor.commit();
Run Code Online (Sandbox Code Playgroud)

  • 如果您需要跟踪发送的每个通知,这是一种相当聪明的方法.可能是这里更聪明的答案之一. (5认同)

Ton*_*aby 10

用这个替换你的线.

notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), notification);
Run Code Online (Sandbox Code Playgroud)


Mun*_*f M 8

我想这会帮助某人..
在下面的代码"not_nu"是一个随机的int .. PendingIntent和Notification具有相同的ID ..所以在每个通知上点击意图将指向不同的活动..

private void sendNotification(String message,String title,JSONObject extras) throws JSONException {
   String id = extras.getString("actionParam");
    Log.e("gcm","id  = "+id);
    Intent intent = new Intent(this, OrderDetailActivty.class);
    intent.putExtra("id", id);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    final int not_nu=generateRandom();
    PendingIntent pendingIntent = PendingIntent.getActivity(this, not_nu /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_cart_red)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(not_nu /* ID of notification */, notificationBuilder.build());
}
public int generateRandom(){
    Random random = new Random();
    return random.nextInt(9999 - 1000) + 1000;
}
Run Code Online (Sandbox Code Playgroud)


小智 5

在这个地方uniqueIntNo放置唯一的整数,如下所示:

mNotificationManager.notify(uniqueIntNo, builder.build());
Run Code Online (Sandbox Code Playgroud)


小智 5

notificationManager.notify(0, notification);
Run Code Online (Sandbox Code Playgroud)

输入此代码而不是 0

new Random().nextInt() 
Run Code Online (Sandbox Code Playgroud)

就像下面一样,它对我有用

notificationManager.notify(new Random().nextInt(), notification);
Run Code Online (Sandbox Code Playgroud)


Kar*_*k s 3

我这样解决了我的问题...

/**
     * Issues a notification to inform the user that server has sent a message.
     */
    private static void generateNotification(Context context, String message,
            String keys, String msgId, String branchId) {
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        NotificationCompat.Builder nBuilder;
        Uri alarmSound = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        nBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Smart Share - " + keys)
                .setLights(Color.BLUE, 500, 500).setContentText(message)
                .setAutoCancel(true).setTicker("Notification from smartshare")
                .setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })
                .setSound(alarmSound);
        String consumerid = null;
        Integer position = null;
        Intent resultIntent = null;
        if (consumerid != null) {
            if (msgId != null && !msgId.equalsIgnoreCase("")) {
                if (key != null && key.equalsIgnoreCase("Yo! Matter")) {
                    ViewYoDataBase db_yo = new ViewYoDataBase(context);
                    position = db_yo.getPosition(msgId);
                    if (position != null) {
                        resultIntent = new Intent(context,
                                YoDetailActivity.class);
                        resultIntent.putExtra("id", Integer.parseInt(msgId));
                        resultIntent.putExtra("position", position);
                        resultIntent.putExtra("notRefresh", "notRefresh");
                    } else {
                        resultIntent = new Intent(context,
                                FragmentChangeActivity.class);
                        resultIntent.putExtra(key, key);
                    }
                } else if (key != null && key.equalsIgnoreCase("Message")) {
                    resultIntent = new Intent(context,
                            FragmentChangeActivity.class);
                    resultIntent.putExtra(key, key);
                }.
.
.
.
.
.
            } else {
                resultIntent = new Intent(context, FragmentChangeActivity.class);
                resultIntent.putExtra(key, key);
            }
        } else {
            resultIntent = new Intent(context, MainLoginSignUpActivity.class);
        }
        PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
                notify_no, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        if (notify_no < 9) {
            notify_no = notify_no + 1;
        } else {
            notify_no = 0;
        }
        nBuilder.setContentIntent(resultPendingIntent);
        NotificationManager nNotifyMgr = (NotificationManager) context
                .getSystemService(context.NOTIFICATION_SERVICE);
        nNotifyMgr.notify(notify_no + 2, nBuilder.build());
    }
Run Code Online (Sandbox Code Playgroud)