繁体   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