Saturday, June 15, 2013

Generate Notification with sound when alarm received

The exercise "Set alarm on specified date/time with DatePicker/TimePicker" simple display Toast when alarm time reached. To generate Notification with sound when alarm received, generate Notification in AlarmReceiver.java using Notification.Builder() or NotificationCompat.Builder().

This exercise use Android Support library's NotificationCompat.Builder() to supports Android version as old as API level 4. (Right click your project > Android Tools > Add Sipport Library...)

Generate Notification with sound when alarm received


Modify AlarmReceiver.java
package com.example.androiddatepicker;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {
 
 private static final int MY_NOTIFICATION_ID=1;
 NotificationManager notificationManager;
 Notification myNotification;
 private final String myBlog = "http://android-er.blogspot.com/";

 @Override
 public void onReceive(Context context, Intent intent) {
  Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();

     Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
     PendingIntent pendingIntent = PendingIntent.getActivity(
            context, 
            0, 
            myIntent, 
            Intent.FLAG_ACTIVITY_NEW_TASK);
     
     myNotification = new NotificationCompat.Builder(context)
       .setContentTitle("Exercise of Notification!")
       .setContentText("http://android-er.blogspot.com/")
       .setTicker("Notification!")
       .setWhen(System.currentTimeMillis())
       .setContentIntent(pendingIntent)
       .setDefaults(Notification.DEFAULT_SOUND)
       .setAutoCancel(true)
       .setSmallIcon(R.drawable.ic_launcher)
       .build();
     
     notificationManager = 
       (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
     notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
 }

}


download filesDownload the files.

Next:
- Start activity once notification clicked

Related:
Generate Alarm, Notification and Ringtone using RingtoneManager

8 comments:

  1. how to make if notification once clicked not to "myblog".
    but once clicked displays a message, for example "wake up!!"

    ReplyDelete
  2. This was super helpful .Thanx!.. the best set of tutorials iv come across

    ReplyDelete
  3. Hi , there is no sound in your downloadable project. I've executed, toast message appears and everything works but the sound. I'm using the android emulator.

    TYIA

    ReplyDelete
  4. How to store the date and time in sharedPreference database?

    ReplyDelete
  5. Can we do this whole alarm application inside the alert dialog box?
    I mean can we prompt to user for setting the date and time in an alert dialog box?

    ReplyDelete
  6. You saved my time mate Thanks :))

    ReplyDelete

  7. could make an alarm to notify from raspberry pi to android some event

    ReplyDelete