如何在两个不同的时间发出两个不同的通知?

时间:2015-08-13 11:55:20

标签: android notifications alarmmanager

我想创建两个不同的通知(具有不同的标题,文本,内容等等)并在一天的两个不同时间发布它们。

这是我的MainActivity代码:

第一次通知:

 receiver.putExtra("which", 0);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(CountdownActivity.this, 0, receiver,0);
 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
 alarmManager.set(AlarmManager.RTC_WAKEUP, after30minutes, pendingIntent);

第二次通知:

receiver.putExtra("which", 1);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(CountdownActivity.this, 0, receiver,0);
AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager2.set(AlarmManager.RTC_WAKEUP, (endInSeconds*1000)-(1800*1000), pendingIntent2);

接收器是此类和接收器类之间的意图(扩展了广播接收器)。 这是我的NotificationReceiver(扩展BroadcastReceiver)代码:

@Override
public void onReceive(Context context, Intent intent)
{
     Intent intentService = new Intent(context, NotificationService.class);
     context.startService(intentService);
}

最后,这是我的NotificationService(扩展服务)代码:

@Override
    public void onStart(Intent intent, int startId)
    {
        super.onStart(intent, startId);

        //preparazione variabili
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        notificationManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
        countdown = new Intent(this.getApplicationContext(), CountdownActivity.class);
        pendingIntent = PendingIntent.getActivity( this.getApplicationContext(),0, countdown,PendingIntent.FLAG_UPDATE_CURRENT);

        //costruzione notifica
        if(intent.getExtras().getInt("which") == 0) //1 ora
        {
            notification = new Notification.Builder(getApplicationContext())
                    .setAutoCancel(true)
                    .setContentTitle("1 ora")
                    .setContentText("contenuto")
                    .setLargeIcon(bm)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setSound(alarmSound)
                    .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
                    .setLights(Color.BLUE, 3000, 3000)
                    .setContentIntent(pendingIntent)
                    .build();
        }
        else if(intent.getExtras().getInt("which") == 1) //mezz'ora
        {
            notification = new Notification.Builder(getApplicationContext())
                    .setAutoCancel(true)
                    .setContentTitle("Ole")
                    .setContentText("contenuto")
                    .setLargeIcon(bm)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setSound(alarmSound)
                    .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
                    .setLights(Color.BLUE, 3000, 3000)
                    .setContentIntent(pendingIntent)
                    .build();
        }

        //operazioni finali
        countdown.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }

EDIT 我解决了在NotificationService类中倒计时的问题。

1 个答案:

答案 0 :(得分:0)

在您致电notificationManager.notify(0, notification);的地方,您应该传递一个唯一的ID,而不是0。因此,例如,传递intent.getExtras().getInt("which")的值就足够了。大多数情况下,我将这些ID声明为类中的public static final int

正如文档陈述NotificationManager#notify(int, android.app.Notification)

  

发布要在状态栏中显示的通知。如果您的应用程序已经发布了具有相同ID的通知但尚未取消,则该通知将被更新的信息替换。

http://developer.android.com/reference/android/app/NotificationManager.html