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...)
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 the files.
Next:
- Start activity once notification clicked
Related:
- Generate Alarm, Notification and Ringtone using RingtoneManager
how to make if notification once clicked not to "myblog".
ReplyDeletebut once clicked displays a message, for example "wake up!!"
Please read Start activity once notification clicked.
ReplyDeleteThis was super helpful .Thanx!.. the best set of tutorials iv come across
ReplyDeleteHi , 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.
ReplyDeleteTYIA
How to store the date and time in sharedPreference database?
ReplyDeleteCan we do this whole alarm application inside the alert dialog box?
ReplyDeleteI mean can we prompt to user for setting the date and time in an alert dialog box?
You saved my time mate Thanks :))
ReplyDelete
ReplyDeletecould make an alarm to notify from raspberry pi to android some event