Search code examples
androidnotificationsbroadcastreceiveralarmmanager

Display notifications on a particular time


I would like to display notification on a particular time and daily routine (i.e at 8'o clock morning). I am using broadcast receiver, alarmmanager to display notifications. But the problem is, I am not getting the notifications displayed on mobile. I've added wake lock permissions in manifest file also. Help me please Thank you

MainActivity.java

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 8);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 0);

    Intent notificationmassage = new Intent(getApplicationContext(),NotificationClass.class);

  //This is alarm manager
  PendingIntent pi = PendingIntent.getService(this, 0 , notificationmassage, PendingIntent.FLAG_UPDATE_CURRENT);
  AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
  am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
          AlarmManager.INTERVAL_DAY, pi);

  Toast.makeText(this, "Start Alarm", Toast.LENGTH_LONG).show();

}

NotificationClass.java

public class NotificationClass extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, MainActivity.class), 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                                                .setSmallIcon(R.drawable.ic_launcher)
                                                .setContentTitle("Text1")
                                                .setContentText("Text2 ");
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager ;
        mNotificationManager= (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1,mBuilder.build());

    }
}

manifest:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.WAKE_LOCK"/>


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <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>

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

UPDATE: - now the issues was when ever I open the Application notification arises. For Ex: Alarm Set to 8.AM, whenever I open the app after 8 AM,the notification arises..even though the current time is > 8 AM. How to solve that?


Solution

  • In your activity Replace following

        PendingIntent pi = PendingIntent.getService(this, 0, notificationmassage, PendingIntent.FLAG_UPDATE_CURRENT);
    

    with

        PendingIntent pi = PendingIntent.getBroadcast(this, 0, notificationmassage, PendingIntent.FLAG_UPDATE_CURRENT);