It's a example of using Notification.Builder, modified from the previous exercise "schedule a repeating alarm".
Change the code of AlarmReceiver.java in "schedule a repeating alarm", to generate Notificaton.
package com.exercise.AndroidTime;
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.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
buildNotification(context);
}
private void buildNotification(Context context){
NotificationManager notificationManager
= (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
Intent intent = new Intent(context, AndroidTimeActivity.class);
PendingIntent pendingIntent
= PendingIntent.getActivity(context, 0, intent, 0);
builder
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("ContentTitle")
.setContentText("ContentText")
.setContentInfo("ContentInfo")
.setTicker("Ticker")
.setLights(0xFFFF0000, 500, 500) //setLights (int argb, int onMs, int offMs)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
Notification notification = builder.getNotification();
notificationManager.notify(R.drawable.ic_launcher, notification);
}
}
Please notice that you have to change your project to target API Level 11, in order to use Notification.Builder.
Download the files.
Very useful guide, worked like a charm!
ReplyDeleteQuick question, if I wanted to put an Extra to the intent, would the PendingIntent structure change?
Note: "incoming body" refers to the "OriginatingAdress" and is of the type String.
Code here:
private void buildNotification(Context context){
NotificationManager notificationManager = (NotificationManager)context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
Intent intent = new Intent(context, SMSComposer.class);
intent.putExtra("MESSAGE", incomingBody);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(incomingBody)
.setTicker(ticker + messageType)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
Notification notification = builder.getNotification();
notificationManager.notify(R.drawable.ic_launcher, notification);
}
Hello Daniel Alvarado,
ReplyDeleteIn my understanding, the PendingIntent structure is same.
i have problem with this code
ReplyDeleteNotification nNotify = new Notification.Builder(this)
the Builder always in red colour
can u help me..
i'm using the android studio
Hello sir,
ReplyDeletei would like to ask how do i input multiple alarm into the codes using pending intent.
Your tutorials have been most helpful.
This blog always make my project more easy, keep writing, your blog most helpful.
ReplyDelete