【问题标题】:Create multi Notification With Different Intent创建具有不同意图的多通知
【发布时间】:2017-04-13 10:23:06
【问题描述】:

我会尝试每次使用不同的参数创建多本地通知这是我创建通知的代码:

 public void setNotficition(int Time,String ProdName,String ProdDesc,String ProdID){
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
    notificationIntent.addCategory("android.intent.category.DEFAULT");
    notificationIntent.putExtra("ProdName",ProdName);
    notificationIntent.putExtra("ProdDesc",ProdDesc);
    notificationIntent.putExtra("ProdID",ProdID);

    PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, Time);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),broadcast);
}

这是我的 BroadcastReceiver 代码:

    @Override
public void onReceive(Context context, Intent intent) {
    String ProdName= intent.getStringExtra("ProdName");
    String ProdDesc= intent.getStringExtra("ProdDesc");
    String ProdID= intent.getStringExtra("ProdID");

    int ID = Integer.parseInt(ProdID);
    Intent notificationIntent = new Intent(context, NotificationActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(NotificationActivity.class);
    stackBuilder.addNextIntent(notificationIntent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

        Notification notification = builder.setContentTitle(ProdName)
                .setContentText(ProdDesc)
                .setTicker("New Message Alert!")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent).build();
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), notification);
}

每次最后一次通话时

【问题讨论】:

标签: java android notifications local


【解决方案1】:

通知id 在您的应用程序中应该是唯一的。

如果您已发布具有相同id 的通知 申请且尚未取消,将由 更新信息。

NotificationManager notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);    
notiManager.notify(UNIQUE_ID, notification);

如果您使用PendingIntent.getBroadcast() 方法,请使用不同的requestCode 进行不同的通知:

Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
notificationIntent.putExtra("ProdName",ProdName);
notificationIntent.putExtra("ProdDesc",ProdDesc);
notificationIntent.putExtra("ProdID",ProdID);

PendingIntent broadcast = PendingIntent.getBroadcast(this, REQUEST_CODE, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

希望这会有所帮助!

【讨论】:

  • 我认为它需要工作,但我收到此错误:错误:(36、62)错误:不兼容的类型:AlarmReceiver 无法转换为上下文
  • 我通过了我的上下文,但它仍然只在最后一次推送时工作
  • 你使用了不同的request_code和不同的notification_id吗?
  • 是的,我把(int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE)
  • 使用不同的请求代码 >> PendingIntent.getBroadcast(this, UNIQUE_REQUEST_CODE, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
【解决方案2】:

您可以通过每次更改通知 ID 来创建多个通知。

PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

getBroadcast 调用的第二个参数是“通知 id”(在您的情况下为 100)。在生成多个通知的情况下,只需使用不同的不同通知 id。

希望对你有帮助:-)

【讨论】:

  • 我在“this”上得到错误 -Error:(36, 62) 错误:不兼容的类型:AlarmReceiver 无法转换为上下文
  • 传递你的“context”对象而不是“this”。
  • :(我通过了我的上下文,但它仍然只在最后一次推送中起作用
  • 使用 new Random().nextInt(0, 1000) 而不是 100。“仅用于测试目的”
【解决方案3】:

一个通知被创建并链接到一个 id,这个 id 可用于修改或更新现有通知,天气你正在改变意图或只是堆栈的行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    相关资源
    最近更新 更多