簡體   English   中英

Android設置未來通知alarmmanager問題不觸發

[英]android set future notification alarmmanager issue not fire

我的應用程序需要將來在不同時間進行通知。 因此,我借助此鏈接實現了以下代碼。

但是最后我的結論是,此代碼不會觸發所有通知,有時將來的任何通知都不會觸發。

應用程序是客戶端-服務器基礎應用程序,因此日期時間從服務器接收。 例子如下。

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>

更改

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM