简体   繁体   中英

Android Notification using alarm manager not working

I have been trying to get notifications to launch on Android at the correct time. I want them to go off 2 mins in the future from App launch. If I can get it working here I can easily get it working where I really need to do it. The logs show that the needed lines of code are running but the notification never launches. I am up to over 8 hours of trying to debug this and getting no where. Any help would be great.

Here is the Reminder code I have created:

    final PendingIntent pi = PendingIntent.getBroadcast(this.mContext,
            0,
            i,
            PendingIntent.FLAG_ONE_SHOT);

    TimeZone timeZone= TimeZone.getDefault();
    Calendar time= Calendar.getInstance(timeZone);
    time.add(Calendar.MINUTE, 2);

    this.mAlarmManager.set(AlarmManager.RTC_WAKEUP,
            time.getTimeInMillis(),
            pi);

    Log.d ("setReminder",time.getTime().toLocaleString());

Now below is my OnAlarmRecieve code that is never running 2 mins later according to the logs. The class extends BroadcastReceiver

@Override
public void onReceive(final Context context,
        final Intent intent) {
    Log.d(TAG, "Recieved wake up cal from Alarm Manger");


    final String tableName = intent.getStringExtra(IntentExtraStringStorage.TABLE_NAME);
    final long rowID = intent.getLongExtra(IntentExtraStringStorage.ROW_ID,
            -1);
    final String titleString = intent.getStringExtra(IntentExtraStringStorage.NOTIFICATION_TITLE);
    final String notificationString=intent.getStringExtra(IntentExtraStringStorage.NOTIFICATION_NOTE);

    WakeUpReminderIntentService.acuireStaticLock(context);

    /*if (tableName== Task.TABLE_NAME) {
        launchTaskView(context, rowID);
    }
    else {
        Log.e (TAG, "Did not Launch");
        Toast.makeText(context, "Did not work right", Toast.LENGTH_SHORT).show();
    }*/
    Intent i = new Intent (context, ReminderService.class);
    i.putExtra("taskID", rowID);
    i.putExtras(intent.getExtras());
    Log.d(TAG, "Launched task");
    //TODO Added code to build the screen correctly
    //FIXIT this need to be finished.
    context.startService(i);
}]=

The problem was not putting the OnAlarmReciever in the mainfest as a receiver, and there was something not being put into the services as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM