Meine anwendung erfordern zukünftige benachrichtigung zu unterschiedlichen zeitpunkten. Also habe ich unten Code mit Hilfe von this link implementiert.android set zukünftige benachrichtigung alarmmanager ausgabe nicht feuer
Aber am Ende meiner Schlussfolgerung ist dieser Code nicht alle Benachrichtigungen ausgelöst, manchmal wird keiner der zukünftigen Benachrichtigung ausgelöst werden.
Anwendung ist Client-Server-Basis-App so Datum-Uhrzeit vom Server erhalten. Beispiel ist wie folgt.
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>
Wann Sie eine Benachrichtigung schießen wollen? – Nisarg
@Nisarg bitte überprüfen Sie die aktualisierte Frage –