Search code examples
androidbroadcastreceiverandroid-notification-bar

NotificationBar entry is not appearing at defined time


I want to define a time when the notification shall be appear so i followed this tutorial

http://karanbalkar.com/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/

I tested the code in MyAlarmService.onStart and this eams to work, I get the notification. So it seems the BroadcastReceiver is not work properly.

my code looks like this

MainActivity:

/***********************************************************
 * Notification Bar Reminder setup
 **********************************************************/


Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.MONTH, 12);
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.DAY_OF_MONTH, 20);

calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 51);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.AM);

Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

}

MyReceiver

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;



public class MyReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
       Intent service1 = new Intent(context, MyAlarmService.class);
       context.startService(service1);

    }  
}

MyAlarmService:

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;


public class MyAlarmService extends Service
{

   private NotificationManager mManager;

    @Override
    public IBinder onBind(Intent arg0)
    {
       // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate()
    {
       // TODO Auto-generated method stub 
       super.onCreate();
    }

   @SuppressWarnings("static-access")
   @Override
   public void onStart(Intent intent, int startId)
   {
       super.onStart(intent, startId);
       /*FR: this code works if you copy it direct to the mainActivity */
       mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
       Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);

       Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());
       intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

       PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
       notification.flags |= Notification.FLAG_AUTO_CANCEL;
       notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);

       mManager.notify(0, notification);
    }

    @Override
    public void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}

manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.whoseturn"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/dummphoto"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.Holo.Light.DarkActionBar">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyAlarmService"
                 android:enabled="true" />

        <receiver android:name=".MyReceiver"/>
    </application>

</manifest>

Solution

  • Calendar.MONTH is zero-based. That is, Calendar.JANUARY == 0. It's better to use the class constants; e.g. Calendar.DECEMBER. Also, Calendar.HOUR_OF_DAY is based in the 24-hour clock.

    I would also point out that you're setting the alarm with AlarmManager.RTC, which will not wake-up your device to fire it. If that behavior is what you need, use AlarmManager.RTC_WAKEUP instead.

    A couple of notes for testing:

    • Calendar.getInstance() returns an instance with the current date and time already set. You need only set those components that differ from right now.

    • If an alarm is set at or before the current time, it will fire immediately. This is a good test to make sure the Receiver is setup correctly.