How to trigger a broadcast receiver from notification? - android

I want to trigger a Broadcast receiver from the notification. When I click on a button which is on the notification it shows this error:
"Unable to instantiate receiver com.example.testservice.myBroad:
java.lang.ClassCastException: com.example.testservice.myBroad cannot
be cast to android.content.BroadcastReceiver"
**updated/ edited and its working now **
Can you please help to handle 2 buttons from notification to broadcast receiver?
How can I pass extra value from notification broadcast trigger to a receiver that whether its play button pressed or pause?
Now my button working but when I click on notification text it does not take me into my activity. Any help?
I write this code for 2 buttons with extra on intent.
RemoteViews layout = new RemoteViews(getPackageName(), R.layout.notification);
layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
Intent clickIntent = new Intent();
clickIntent.putExtra("button","pause");
clickIntent.setAction(ACTION_DIALOG);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, clickIntent, pendingFlag);
layout.setOnClickPendingIntent(R.id.notification_button,pendingIntent);
builder.setContent(layout);
layout = new RemoteViews(getPackageName(), R.layout.notification);
layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
Intent click = new Intent();
clickIntent.putExtra("Button","play");
clickIntent.setAction(ACTION_DIALOG);
PendingIntent pi1 = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, click, pendingFlag);
layout.setOnClickPendingIntent(R.id.notification_button1,pi1);
builder.setContent(layout);
myBroad receiver file
Bundle extrasBundle = intent.getExtras();
String str = (String) extrasBundle.get("button");
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
context.stopService(new Intent(context, myPlayService.class));
Here is my code:
void showNotification() {
int pendingRequestCode = 0;
int pendingFlag = 0;
final Resources res = getResources();
final NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
Intent intent = new Intent(MainActivity.this,myBroad.class);
PendingIntent pi= PendingIntent.getActivity(this, 0, intent, 0);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_action_search)
.setAutoCancel(true)
.setTicker("this is notification")
.setContentIntent(getDialogPendingIntent("Tapped the notification entry."));
// Sets a custom content view for the notification, including an image button.
RemoteViews layout = new RemoteViews(getPackageName(), R.layout.notification);
layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
Intent clickIntent = new Intent();
clickIntent.setAction(ACTION_DIALOG);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, clickIntent, pendingFlag);
layout.setOnClickPendingIntent(R.id.notification_button,pendingIntent);
builder.setContent(layout);
// Notifications in Android 3.0 now have a standard mechanism for displaying large
// bitmaps such as contact avatars. Here, we load an example image and resize it to the
// appropriate size for large bitmaps in notifications.
Bitmap largeIconTemp = BitmapFactory.decodeResource(res,
R.drawable.notification_default_largeicon);
Bitmap largeIcon = Bitmap.createScaledBitmap(
largeIconTemp,
res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width),
res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height),
false);
largeIconTemp.recycle();
builder.setLargeIcon(largeIcon);
notificationManager.notify(NOTIFICATION_DEFAULT, builder.getNotification());
}
PendingIntent getDialogPendingIntent(String dialogText) {
return PendingIntent.getActivity(
this,
dialogText.hashCode(), // Otherwise previous PendingIntents with the same
// requestCode may be overwritten.
new Intent(ACTION_DIALOG)
.putExtra(Intent.EXTRA_TEXT, dialogText)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
0);
}
myBroad.class
public class myBroad extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "received", Toast.LENGTH_SHORT).show();
context.stopService(new Intent(context, myPlayService.class));
}
}
Manifest file is:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".myBroad">
<intent-filter >
<action android:name="android.intent.action.MEDIA_BUTTON"/>
</intent-filter>
</receiver>
<service android:name="com.example.testservice.myPlayService" android:icon="#drawable/ic_action_search" android:label="#string/app_name" android:enabled="true"/>
</application>

Because myBroad is an activity object, and not a broadcast object...
Instead of extending (inheritance) from activity, and then making an internal class which extends broadcastreceiver, then just inherit directly from broadcastreceiver.

In your method getDialogPendingIntent you should use PendingIntent.getBroadcast instead of PendingIntent.getActivity. This will cause your PendingIntent to push a broadcast.
And fix this:
public class myBroad extends BroadcastReceiver

Exception says it clearly - your myBroad have to extend BroadcastReceiver, not Activity as it does now.
PS: you should consider switching to proper naming convenction. So it should be MyBroad (class) but myBroad (object of that class). i.e.:
MyBroad myBroad = new MyBroad();

Related

SmsManager Pending Intent for Sent and Delivered Not Called [duplicate]

I have download notification in my application. I've added "Cancel" button to NotificationCompat.Builder by calling addAction() method. But button not working on Android O device. When I press "Cancel" button nothing happens. But button working on Android < O.
My Notification:
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, channelId)
.setContentTitle(title)
.setSmallIcon(R.drawable.network_download)
.setContentText(contentText)
.setOngoing(true)
.setContentIntent(null)
.addExtras(idBundle)
.addAction(R.drawable.cancel, context.getString(R.string.cancel), getCancelPendingIntent(context, id))
.setProgress(100, 30, true);
My PendingIntent :
private PendingIntent getCancelPendingIntent(Context context, int id){
return PendingIntent.getBroadcast(
context, id, new Intent("CANCEL_DOWNLOAD").putExtra("id", id), PendingIntent.FLAG_UPDATE_CURRENT);
}
Also I have NotificationReceiver :
public static class NotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ("CANCEL_DOWNLOAD".equals(action) && context != null){
int id = intent.getIntExtra("id", -1);
NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (mgr != null)
mgr.cancel(id);
FtpManager.getInstance(new AppExecutors(), CredentialsManager.getInstance().getCredentials(context))
.cancelDownloading();
}
}
}
In Manifest file I have :
<receiver
android:name="eu.warble.pjapp.util.NotificationsManager$NotificationReceiver"
android:exported="false">
<intent-filter>
<action android:name="CANCEL_DOWNLOAD" />
</intent-filter>
</receiver>
Never use an implicit Intent when an explicit Intent will do. Android O helps enforce this by banning the receipt of implicit Intent broadcasts from manifest-registered receivers.
Step #1: Remove the <intent-filter> from your <receiver> (which also means that you could get rid of android:exported="false", as that is now the default value)
Step #2: Replace new Intent("CANCEL_DOWNLOAD").putExtra("id", id) with new Intent(context, NotificationReceiver.class).putExtra("id", id)
Thank you very much CommonsWare. It works for me. This is my code
var notifyIntent = Intent(context, NotificationBroadcastReceiver::class.java)
notifyIntent.action = "ACTION_UPDATE_BADGE"//hardcode in manifest
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// only for oreo and newer versions
notifyIntent = Intent(context, NotificationBroadcastReceiverAndroidO::class.java)
}
val pendingIntent = PendingIntent.getBroadcast(context, it, notifyIntent, PendingIntent.FLAG_ONE_SHOT)
In manifest
<receiver android:name=".firebase.NotificationBroadcastReceiver">
<intent-filter>
<action android:name="ACTION_UPDATE_BADGE" />
</intent-filter>
</receiver>
<receiver android:name=".firebase.NotificationBroadcastReceiverAndroidO">
<!--Android O not working when having this code
<intent-filter>
<action android:name="ACTION_UPDATE_BADGE" />
</intent-filter>-->
</receiver>
Create 2 classes:
open class NotificationBroadcastReceiver : BroadcastReceiver() {...}
open class NotificationBroadcastReceiverAndroidO : NotificationBroadcastReceiver() {/*do nothing*/}

BroadcastReceiver does not get data from notification button

I am trying to make notification with remove button, notifications are stored in SQLite DB and after the press, I want to remove "record" by id. Firstly I get a notification, store it to db , store unique id and pass it to a method which creates a notification. It gets fine in the method.
public static String CUSTOM_ACTION = "com.example.app.Services.REMOVE";
Intent snoozeIntent = new Intent(this, MyBroadcastReceiver.class);
snoozeIntent.setAction(CUSTOM_ACTION);
snoozeIntent.putExtra("NOT_WORKING", String.valueOf(id));
PendingIntent snoozePendingIntent =
PendingIntent.getBroadcast(this, 0, snoozeIntent, 0);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.ic_stat_bell)
.setContentTitle("Loggly")
.setContentText(messageBody)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.addAction(R.drawable.ic_delete_forever_black_24dp, "Remove", snoozePendingIntent);
then I have a broadcast receiver for handling data from notification. Here is how it looks in my manifest file.
<receiver android:name=".MyBroadcastReceiver" android:exported="false">
<intent-filter>
<action android:name="com.example.app.Services.REMOVE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
And here is the broadcast receiver the value which I got from extras is always null ... I have been trying to find a solution, but with no result.
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String id;
if (extras != null) {
id = extras.getString("NOT_WORKING");
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(ns);
if (notificationManager != null) {
notificationManager.cancel(0);
}
Intent inten = new Intent(context, RemoveService.class);
inten.putExtra("NOT_WORKING", id);
context.startService(inten);
}
}
}
Finally, I am starting intent service, which should delete the "record" from the database, but when the broadcast receiver does not receive id it can't do anything.
I just created a sample notification on my side and the problem is the way you are creating the pendingIntent. Just add the proper flag to the pendingIntent parameter and it will work fine.
PendingIntent snoozePendingIntent = PendingIntent.getBroadcast(this, 0, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
You can use FLAG_ONE_SHOT as well. If it satisfies your use case. You can go through different flags which can be used in PendingIntent

Notification started from service doesn't open the app on click

Good Morning,
I've made an application that implements GpS Location. I have a service that save my location on LocationChanged event. To avoid that Android close my app I start a notification and all works well. But now, I want that when I click on notification from Action Bar the app come back in foreground.I use fragment and the map fragment is called MappaFragment. I read a lot of messages but It seems not resolve my issue. Below my code, any suggestion is appreciated !
Alex
This is my monitoring Service:
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals(Constants.STARTFOREGROUND_ACTION)) {
Log.i(TAG, "Received Start Foreground Intent ");
Intent notificationIntent = new Intent(getApplicationContext(), LocationMonitoringService.class);
Bundle extras = intent.getExtras();
numeroTraccia = extras.getInt("numeroTraccia");
itinerario = extras.getString("itinerarioRiferimento");
notificationIntent.setAction(Constants.MAIN_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
Intent.FLAG_ACTIVITY_CLEAR_TASK);
setGiornoRiferimento(gg.checkDayLong(this));
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
0, new Intent(getBaseContext(),MappaFragment.class), 0);
Bitmap icon = BitmapFactory.decodeResource(getResources(), com.example.alex.myApp.R.drawable.smallicon );
Notification.BigTextStyle bigText = new Notification.BigTextStyle();
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("MyApp")
.setTicker(getString(R.string.track))
.setStyle(bigText.bigText(getString(R.string.track)))
.setSmallIcon(R.drawable.smallicon)
.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
notification);
}
This is my Manifest:
<activity android:name="com.example.alex.myApp.MainActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|screenSize|keyboardHidden" />
<activity
android:name="com.example.alex.myApp.SplashScreen"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.example.alex.myApp.services.MonitoringService"
android:enabled="true"/>
<receiver
android:name="com.example.alex.myApp.RestarterBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:label="RestartWhenStopped">
<intent-filter>
<action android:name="com.example.alex.myApp.ActivityRecognition.RestartSensor"/>
</intent-filter>
</receiver>
Thanks in advance !
Alex
First of all,
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getBaseContext(),MappaFragment.class), 0);
this is incorrect. Fragment can't be opened using intent. You need to launch an activity, then redirect to required fragment.
Next thing is, if app is in background and you click on the notification, it will open launcher activity. So in launcher activity you need to check for bundle object and there you can redirect to particular activity or fragment.
Check this post: Push notification works incorrectly when app is on background or not running and Open specific Activity when notification clicked in FCM it will help you to better understand how FCM works when app is in background and foreground.
Create a BroadCast Receiver like below:
public static class ActionReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("open_activity")) {
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClass(AppContext.getContext(), MappaFragment.class);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent);
}
}
}
And then, Use this notificationIntent.setAction("open_activity");
And Change your PendingIntent to:
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
0, new Intent(AppContext.getContext(), ActionReceiver.class), 0);
NOTE: For USing This you need to Remove These Lines:
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, notification)
What I'm suggesting is to use a BroadcastReceiver to receive whatever you want to do in Your Notification.
Also, If this isn't working try Adding .setAction(Intent.ACTION_MAIN) to Your Notification like Below:
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("MyApp")
.setTicker(getString(R.string.track))
.setStyle(bigText.bigText(getString(R.string.track)))
.setSmallIcon(R.drawable.smallicon)
.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.setOngoing(true)
.setAction("open_activity")
.build();

BroadcastReceiver not receiving when .addAction() is pressed

I'm trying to dismiss a notification from an .addAction() without having to open the app. The problem is when the button is pressed nothing happens, the onReceive() method doesn't trigger.
Here is the code on the MainActivity:
Intent notificationIntent = new Intent(mContext, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("id", SOMENUMBER);
PendingIntent pIntent = PendingIntent.getBroadcast(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext);
notification.setContentTitle("");
notification.setContentText(t);
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setOngoing(true);
notification.addAction(R.mipmap.ic_launcher, "Dismiss", pIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(SOMENUMBER, notification.build());
And on other class I have the reciever:
public class Notification extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(intent.getIntExtra("id", 0));
}
}
And the reciever on the AndroidManifest.xml file:
<receiver android:name=".MainActivity">
<intent-filter>
<action android:name="io.github.seik.Notification" />
</intent-filter>
</receiver>
Your naming conventions are confusing. Android already has a class called Notification, so you probably shouldn't call your receiver Notification :-(
If MainActivity extends Activity then you need to have a manifest entry for it that looks like this:
<activity android:name=".MainActivity"/>
For your BroadcastReceiver, you need a manifest entry like this:
<receiver android:name=".Notification"
android:exported="true"/>
Since you are using an explicit Intent to launch your BroadcastReceiver, you don't need to provide an <intent-filter> for it. Since the BroadcastReceiver will be started by the NotificationManager, you need to make sure that it is exported.
You then need to create the PendingIntent so that it actually launches your BroadcastReceiver, so change this:
Intent notificationIntent = new Intent(mContext, MainActivity.class);
to this:
Intent notificationIntent = new Intent(mContext, Notification.class);

android get preferences from broadcast receiver

Well, i am an Android newbie. I am trying to write an application which has one activity which starts another activity (Preference Activity), and one BoradcastReceiver. They are all in three different files. My question is: How to share Preferences betweens these components, e.g. how to read preferences that are set in Preference Activity from Broadcast Receiver?
I have asked this same exact question before and here is the code I used:
public class BootupReceiver extends BroadcastReceiver {
private static final boolean BOOTUP_TRUE = true;
private static final String BOOTUP_KEY = "bootup";
#Override
public void onReceive(Context context, Intent intent) {
if(getBootup(context)) {
NotificationManager NotifyM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification Notify = new Notification(R.drawable.n,
"NSettings Enabled", System.currentTimeMillis());
Notify.flags |= Notification.FLAG_NO_CLEAR;
Notify.flags |= Notification.FLAG_ONGOING_EVENT;
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
Notify.contentView = contentView;
Intent notificationIntent = new Intent(context, Toggles.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notify.contentIntent = contentIntent;
int HELO_ID = 00000;
NotifyM.notify(HELLO_ID, Notify);
}
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.leozar100.myapp.NotifyService");
context.startService(serviceIntent);
}
public static boolean getBootup(Context context){
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(BOOTUP_KEY, BOOTUP_TRUE);
}
}
The service that I start does nothing I just initiate one because I think it just helps the broadcast receiver work. Also this broadcast receiver is registered in my manifest like so:
<receiver android:name=".BootupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
Which starts on bootup requiring the permission android.permission.RECEIVE_BOOT_COMPLETED
Reference to my question can be found here
P.S. Welcome to stackoverflow

Categories

Resources