Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Notification at a particular time through Alarm Manager

I am trying to create a notification at a particular time. Im creating a broadcast receiver and calling it through the AlarmManager. Problem is that the broadcast is not received and that I am not getting any notifications.

Registering the Broadcast Receiver in the Manifest,

<receiver
        android:name="com.example.android.receivers">
        <intent-filter>
            <action android:name="com.example.android.receivers.AlarmReceiver" />
        </intent-filter>
    </receiver>

This is the Broadcast Receiver,

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context pContext, Intent pIntent) {

//      if (pIntent.getAction().equalsIgnoreCase("")) {
//          
//      }
        Log.d("Alarm Receiver", "onReceive called");        
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(pContext).setSmallIcon(R.drawable.icon).setContentTitle("Test Notification")
                        .setContentText("Test Notification made by Syed Ahmed Hussain");
        Intent resultIntent = new Intent(pContext, CalendarView.class);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(pContext);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(CalendarView.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) pContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, notificationBuilder.build());
    }

}

Code from Main Activity which creates an Alarm.

/**
     * 
     * @param pDate
     * @param pTime
     */
    private void createNotification(String pDate, String pTime) {
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlarmReceiver.class);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
//      alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60 * 1000, alarmIntent);
        alarmManager.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), alarmIntent);
    }

Can anyone please point out and rectify what am I doing wrong? Thank you.

like image 515
Ahmed Avatar asked Nov 13 '13 21:11

Ahmed


People also ask

How do you set a notification at a certain time?

On Android, you will need to open Settings. Then choose Sound>Do Not Disturb>Schedules. On this screen, you have several options to set the days and times for Do Not Disturb, choose which apps to apply it to, and set exceptions.

Can you make a notification into an alarm?

Tap Open Sound NotificationsSettings . Tap Sound Notifications are active. Choose the sounds that you'd like to detect: Smoke and fire alarms.


1 Answers

You have a wrong definition of the Receiver in your Manifest.

Try this:

<receiver android:name="com.example.android.receivers.AlarmReceiver" />

in android:name you have to specify the absolute package+class of the receiver, or the relative from the package="com.example.android" you specified in the root element of the Manifest. e.g:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android" >

Action sections of receivers are to specify which actions to listen. So you can also do it this way:

Create an Intent based on the Action instead of the class

public static final String ACTION = "com.example.android.receivers.NOTIFICATION_ALARM";

private void createNotification(String pDate, String pTime) {
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(ACTION);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    alarmManager.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), alarmIntent);
}

Then you can handle multiple actions in your receiver:

@Override
public void onReceive(Context pContext, Intent pIntent){
        if (pIntent.getAction().equals(ACTION)){
            // Here your handle code
        }
}

But then, you will have to declare your receiver in the manifest this way:

<receiver android:name="com.example.android.receivers">
    <intent-filter>
        <action android:name="com.example.android.receivers.NOTIFICATION_ALARM" />
    </intent-filter>
</receiver>

You can use both ways.

like image 101
nsL Avatar answered Sep 30 '22 11:09

nsL