【问题标题】:How to show different Message with each Notification android?如何使用每个 Notification android 显示不同的消息?
【发布时间】:2013-11-04 11:46:14
【问题描述】:

我正在开发一个 android 应用程序,我想在通知栏中显示一条新消息,其中包含每个新通知。我可以在特定时间显示通知,但我想用不同的消息显示每个通知。

下面我粘贴了在指定时间显示两个通知的示例代码。 我使用了警报管理器类来显示通知。 MainActivity

Calendar calendar = Calendar.getInstance();

     calendar.setTime(new Date(formattedDate1));
     calendar.set(Calendar.MONTH, 9);
     calendar.set(Calendar.YEAR, 2013);
     calendar.set(Calendar.DAY_OF_MONTH, 25);


    calendar.set(Calendar.HOUR_OF_DAY, 13);
    calendar.set(Calendar.MINUTE, 11);
    calendar.set(Calendar.SECOND, 0);       

    Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
    myIntent.putExtra("myIntent", "Notification1");
    myIntent.setType("intent1");
    pendingIntent1 = PendingIntent.getBroadcast(MainActivity.this, 0,
            myIntent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(),
            pendingIntent1);

    Calendar calendar_new = Calendar.getInstance();

    // calendar_new.setTime(new Date(, month, day))

    calendar_new.set(Calendar.HOUR_OF_DAY, 13);
    calendar_new.set(Calendar.MINUTE, 12);
    calendar_new.set(Calendar.SECOND, 0);

    Intent myIntentnew = new Intent(MainActivity.this, MyReceiver.class);
    myIntentnew.setType("intent2");
    myIntentnew.putExtra("myIntentnew", "Notification2");
    pendingIntent2 = PendingIntent.getBroadcast(MainActivity.this, 1,
            myIntentnew, 0);

    alarmManager.set(AlarmManager.RTC, calendar_new.getTimeInMillis(),
            pendingIntent2);


} // end onCreate

通知服务

private NotificationManager mManager;
Notification notification;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
}

@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    mManager = (NotificationManager) this.getApplicationContext()
            .getSystemService(
                    this.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),
            MainActivity.class);

    notification = new Notification(R.drawable.ic_launcher,
            "This is a test message!", System.currentTimeMillis());


    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
            this.getApplicationContext(), 0, intent1,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notification.setLatestEventInfo(this.getApplicationContext(),
            "We succeded", "hi!", pendingNotificationIntent);

    mManager.notify(0, notification);
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();

    mManager.cancelAll();
}

如果有人可以帮助我解决这个问题。提前致谢

【问题讨论】:

  • “我想用不同的消息显示每个通知。”你这是什么意思?您是在生成硬编码通知还是从服务器获取通知??
  • 现在我正在生成硬编码,

标签: android notifications


【解决方案1】:

改变

 mManager.notify(0, notification); 

  mManager.notify(count , notification);

其中 count 每次增加 1 。

   mManager.notify(0, notification) 

每次更换消息

   Intent serviceIntent = new Intent(YourService.class.getName())
   serviceIntent.putExtra("UserID", "123456");
   context.startService(serviceIntent);

当服务启动时,它的 onStartCommand() 方法将被调用,因此在此方法中,您可以从 Intent 对象中检索值 (UserID),例如

public int onStartCommand (Intent intent, int flags, int startId){

String userID = intent.getStringExtra("UserID");

return START_STICKY;
  }

【讨论】:

  • 我确实尝试了您的解决方案,但它没有显示新消息,作为更新,我在这里传递静态日期值和静态消息,但是一旦我能够显示新消息,我将从数据库中获取新消息.
  • 我尝试了您的解决方案,它显示了第一条消息,但在下一个时间间隔它给出了一个空指针异常。任何想法。我在 MainActivity 中有意传递我的自定义消息,上面显示的代码
  • 你能从 onStartCommand() 获取数据吗?在您的服务中覆盖它stackoverflow.com/questions/3293243/…
  • 是的,我可以从 onStartCommand() 获取数据,我也解决了 NullPointerException 但我仍然无法在第二个通知中显示自定义消息
【解决方案2】:

你可以改变这个 mManager.notify(0, notification); to mManager.notify((int) when, notification);

where long when = System.currentTimeMillis();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 2017-03-02
    • 2021-09-24
    • 2017-08-03
    • 2014-07-04
    相关资源
    最近更新 更多