Is it possible to have a notification start a broadcast receiver?
I tried this code but it doesnt work.
Notification is created but when I click on it nothing happens.
NOTE: When I change the notificationIntent to point from MyBroadcastReceiver.class to an activity (like MainActivity.class) it works fine.
Notification creation:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(
Context.NOTIFICATION_SERVICE);
int notificationIconId = XXXXXX
Notification notification = new Notification(
notificationIconId,
XXXXXX,
System.currentTimeMillis()
);
CharSequence contentTitle = XXXXXXX
CharSequence contentText = XXXXXX
Intent notificationIntent = new Intent(context,MyBroadcastReceiver.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(1,notification);
Here is the BroadcastReceiver
public static class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/*
*/
}
}
Inside AndroidManifest.xml
<receiver android:name=".MyBroadcastReceiver" />
onReceive. This method is called when the BroadcastReceiver is receiving an Intent broadcast. During this time you can use the other methods on BroadcastReceiver to view/modify the current result values.
getStringExtra("message"); And then you will use message as you need. If you simply want the ReceiveText activity to show the message as a dialog, declare <activity android:theme="@android:style/Theme. Dialog" /> in your manifest for ReceiveText and then set the message to a textview in the activity.
Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.
Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.
From your code...
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
When creating a PendingIntent
targetting a BroadcastReceiver
, you have to use getBroadcast(...)
and not getActivity(...)
.
See PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags)
Also, don't create your Intent
like this...
Intent notificationIntent = new Intent(context,MyBroadcastReceiver.class);
That is an explicit Intent
which targets a specific class (used for starting a specific Activity
class usually).
Instead create a 'broadcast' Intent
with an 'action' such as...
Intent notificationIntent = new Intent(MyApp.ACTION_DO_SOMETHING);
You'll also need to specify a <intent-filter>
section for the <receiver android:name=".MyBroadcastReceiver" />
section of your manifest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With