我必须管理该图中显示的一组通知(不要担心重复项):
我必须为每个项目发送一个通知,管理何时删除通知(因此应用程序没有发送通知)以及“通知”的更改时间(日期和时间)。 该应用程序处于后台/睡眠状态时,如何管理所有这些内容?
这是我在CoreActivity.java中的方法:
public void notifyPush(Date datetime)
{
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//Intent intent = new Intent(context,NotificationReceiver.class);
//PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
//nuovo codice
Intent intent = new Intent(CoreActivity.this , NotificationReceiver.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(CoreActivity.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent); //set repeating every 24 hours
//fine nuovo codice
Notification notification = new Notification.Builder(context)
.setSound(soundUri)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Notifica")
.setContentText("Abbiamo le notifiche").
addAction(R.drawable.ic_launcher_foreground, "Open", pendingIntent)
.addAction(0, "Remind", pendingIntent).build();
NotificationManager notifManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notifManager.notify(0, notification);
}
NotificationReceiver.java
package com.example.msnma.movienotifier.notify;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
import com.example.msnma.movienotifier.R;
public class NotificationReceiver extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_core);
TextView tv = new TextView(this);
tv.setText("Notification Text");
setContentView(tv);
}
}