我的申请需要在不同时间提供日后通知。 所以我在this link的帮助下实现了以下代码。
但最后我得出的结论是,此代码并未触发所有通知,有时未来的通知都不会触发。
应用程序是客户端 - 服务器基础应用程序,因此从服务器接收日期时间。示例如下。
SimpleDateFormat simpleDateFormatDateTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.US);
Date eventAlertDateTime = simpleDateFormatDateTime.parse("2016-09-08 16:41:00.0");
Calendar calendar = Calendar.getInstance();
calendar.setTime(eventAlertDateTime);
setInternalBroadcastNotification(eventAlertDateTime, "Your brunch is readyon 2nd floor.");
eventAlertDateTime = simpleDateFormatDateTime.parse("2016-09-09 16:41:00.0");
calendar.setTime(eventAlertDateTime);
setInternalBroadcastNotification(eventAlertDateTime, "Your brunch is readyon 2nd floor.");
public void setInternalBroadcastNotification(Date eventAlertDateTime, String eventNotificationMessage) {
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(eventAlertDateTime);
long timeDifference = calendar.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
if (timeDifference > 0) {
Intent receiverServiceIntent = new Intent(mContext, InternalBroadcastReceiver.class);
receiverServiceIntent.putExtra("eventNotificationMessage", eventNotificationMessage);
PendingIntent pendingIntent = PendingIntent.getService(mContext, (int) eventAlertDateTime.getTime(), receiverServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
} else {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
public class InternalBroadcastReceiver extends Service {
public InternalBroadcastReceiver() {
}
public static final int NOTIFICATION_ID = 1;
public class ServiceBinder extends Binder {
InternalBroadcastReceiver getService() {
return InternalBroadcastReceiver.this;
}
}
@Override
public void onCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
showNotification(intent.getStringExtra("eventNotificationMessage"));
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new ServiceBinder();
private void showNotification(String message) {
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
//DO NOTHING ON NOTIFICATION
Intent callingIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, callingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
//FROM LOLLIPOP SmallIcon MUST BE WHITE
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.ic_notification);
} else {
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
}
notificationBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
.setTicker(getString(getApplicationInfo().labelRes))
.setContentTitle(getResources().getString(getApplicationInfo().labelRes))
.setContentText(message)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
// Stop the service when we are finished
stopSelf();
}
}
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<service
android:name=".utils.InternalBroadcastReceiver"
android:enabled="true"
android:exported="true"></service>
答案 0 :(得分:1)
更改
PendingIntent pendingIntent = PendingIntent.getService(mContext, (int) eventAlertDateTime.getTime(), receiverServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
到
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
PendingIntent pendingIntent = PendingIntent.getService(mContext, n , receiverServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
和
通过你的未来时间:
alarmManager.set(AlarmManager.RTC_WAKEUP, eventAlertDateTime.getTime(), pendingIntent);