【发布时间】:2018-08-13 23:37:12
【问题描述】:
大约一周以来,我一直在尝试为我的日历日计划应用程序发送通知。只要应用在最近的任务中,通知就可以正常工作,但删除后它根本就不起作用。
最初我尝试使用调用 BroadcastReciever 的简单 AlarmManager,但我知道它不提供此功能,所以我决定学习和使用 Service。
一开始我尝试了 IntentService,现在我尝试了 Service。我没有收到任何日志错误,所以我猜我对这个系统的理解存在根本问题,所以如果你能启发我,我将不胜感激。
这是我的班级整体处理通知:
package com.example.android.calendar.Helpers;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import com.example.android.calendar.Model.Event;
import com.example.android.calendar.R;
import java.util.Calendar;
import java.util.HashMap;
import java.util.UUID;
public class NotificationService extends Service {
private static Context mContext;
private static final String NOTIFICATION_CHANNEL_ID = "notificationChannelId";
private static final String NOTIFICATION_CHANNEL_NAME = "eventsNotificationChannel";
public static final String EX_ID = "extraId";
public static final String ACTION_START_SERVICE = "startService";
public static final String ACTION_NOTIFY_ON_TIME = "notifyOnTime";
private static HashMap<UUID, Notification> notifications = new HashMap<>();
public static int mCounter = 0;
public NotificationService(){
super();
}
@Override
public void onCreate(){
super.onCreate();
createNotificationChannel(this.getApplicationContext());
}
@Override
public int onStartCommand(final Intent intent, int flags, int startId){
super.onStartCommand(intent, flags, startId);
new Thread(new Runnable() {
@Override
public void run() {
if(intent.getAction() == ACTION_NOTIFY_ON_TIME){
UUID id = (UUID) intent.getSerializableExtra(EX_ID);
Notification notification = notifications.get(id);
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(mCounter++, notification);
}
try{
Thread.sleep(1000);
} catch (InterruptedException e){
e.printStackTrace();
}
}
}).start();
return IntentService.START_REDELIVER_INTENT;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void createNotificationChannel(Context context){
mContext = context;
NotificationManager mNM = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 100});
notificationChannel.enableLights(true);
mNM.createNotificationChannel(notificationChannel);
}
public void createNotification(int minutesBefore, Event event){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID).
setSmallIcon(R.drawable.ic_launcher_foreground).setContentTitle(event.getLabel()).
setContentText(event.getComment()).setAutoCancel(true);
// If an event is edited, remove existing notification
if(notifications.get(event.getId()) != null)
notifications.remove(event.getId());
Calendar mCalendar = Calendar.getInstance();
notifications.put(event.getId(), mBuilder.build());
mCalendar.setTime(event.getTime());
mCalendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(mContext, NotificationService.class);
intent.putExtra(EX_ID, event.getId());
intent.setAction(ACTION_NOTIFY_ON_TIME);
PendingIntent notificationIntent = PendingIntent.getService(mContext, (int)System.currentTimeMillis(),
intent, PendingIntent.FLAG_CANCEL_CURRENT);
long triggerInMills = mCalendar.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC, System.currentTimeMillis() + triggerInMills, notificationIntent);
}
public void cancelNotification(Event event, PendingIntent notificationIntent){
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(notificationIntent);
notifications.remove(event.getId());
}
}
很确定这无关紧要,但这里是我创建和启动服务的地方:
notificationService = new NotificationService();
Intent startServiceIntent = new Intent(getActivity(), NotificationService.class);
startServiceIntent.setAction(NotificationService.ACTION_START_SERVICE);
getActivity().startService(startServiceIntent);
【问题讨论】:
-
查看一个信号。
-
信号是什么意思?
-
'One Signal' onesignal.com
标签: android android-service android-notifications