In the messaging program I made, I used pending intent to receive the message sending and delivery report. When I send several messages and wait for the delivery report, the program does not recognize which message the received pending intent belongs to.
My code is:
sentSMS = PendingIntent.getBroadcast(this, msgId(), new Intent(SMS_SENT), 0); deliverSMS = PendingIntent.getBroadcast(this, msgId(), new Intent(SMS_DELIVERED), 0);
According to what I searched for, I have to use the request code parameter for this, for each message I have assigned a request code **(the ID of each message from the database) **in pending intent, but I don't know how to receive it in on receive.
thanks
Also i tried this:
Intent deliveryIntent = new Intent();
deliveryIntent.putExtra("id", msgId());
deliverSMS = PendingIntent.getBroadcast(this, msgId(), deliveryIntent, 0);
//************
Intent sendIntent = new Intent();
sendIntent.putExtra("id", msgId ());
sentSMS = PendingIntent.getBroadcast(this, msgId(), sendIntent, 0);
But I did not receive anything in extra
TThanks 🙏
When you create the PendingIntent, you need to specify PendingIntent.UPDATE_CURRENT as the last parameter of the getBroadcast() call. This will ensure that each PendingIntent has the correct "id".
In onReceive() you should get the broadcast Intent and it should contain the "extra" named "id" with the message ID.
However, if you create the Intent without an ACTION and without a component or package name, your onReceive() will never get called. Please edit your question and show how you have set up your BroadcastReceiver.
Related
I have app which sends notifications, I used this code for pending intent
Intent myIntent = new Intent(getApplicationContext(),MainActivity.class);
myIntent.putExtra("link",Link);
PendingIntent intent2 =
PendingIntent.getActivity(getApplicationContext(),1,myIntent,
PendingIntent.FLAG_ONE_SHOT);
and it worked nice for the first time, but I use this piece of code inside a method that invokes every 1 minute.
The problem is that the link variable changes from one to another.
and when I get the data in the MainActivity I found the last link only, all the notifications created has the last link.
and I don't know how to avoid this.
You are sending the same request code for your pending intents. These codes are defined as the 2nd parameter of your Pending intent declaration
Change
PendingIntent intent2 =
PendingIntent.getActivity(getApplicationContext(),1,myIntent,
PendingIntent.FLAG_ONE_SHOT);
To:
PendingIntent intent2 =
PendingIntent.getActivity(getApplicationContext(),UNIQUE_INT_VALUE_FOR_EVERY_CALL,myIntent,
PendingIntent.FLAG_ONE_SHOT);
If you use the same id, the intent will be reused and you will only get the last data rather than getting new data for every call.
Try This,
PendingIntent contentIntent = PendingIntent.getActivity(GCMNotificationIntentService.this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
In Kotlin
Make a notification
val builder = NotificationCompat.Builder(context, CHANNEL_ID)
For one and more just change the request Code number:
val pendingIntent = PendingIntent.getActivity(context,
System.currentTimeMillis().toInt(),intent, 0)
then
builder.setContentIntent(pendingIntent)
So I have a service, in the onCreate() I setup 3 pending intents to start the same service, each having different extra data to specify the action. I'm creating a notification, and I want to use one pending intent as the click action, one as the dismiss action, and the third is for an alarm.
Intent iCancel = new Intent(this, BootService.class);
Intent iAlarm = new Intent(this, BootService.class);
Intent iDismiss = new Intent(this, BootService.class);
// each will have actions
iAlarm.putExtra(INTENT_ACTION, INTENT_ALARM_FIRED);
iCancel.putExtra(INTENT_ACTION, INTENT_CANCEL);
iDismiss.putExtra(INTENT_ACTION, INTENT_DISMISS);
PendingIntent piCancel = PendingIntent.getService(
this,
0,
iCancel,
Intent.FILL_IN_DATA);
mPiAlarm = PendingIntent.getService(this, 0, iAlarm, Intent.FILL_IN_DATA);
PendingIntent piDismiss = PendingIntent.getService(this, 0, iDismiss, Intent.FILL_IN_DATA);
mNotifyBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_about)
.setContentTitle(getString(R.string.app_name))
.setContentIntent(piCancel)
.setDeleteIntent(piDismiss);
The problem is all pending intents seem that have the same intent extra data, so when onStartCommand is launched no matter whether the notification was clicked or dismissed or neither, constant INTENT_CANCEL is received from intent.getIntExtra(INTENT_ACTION)
I believe it has something to do with the flags used in PendingIntent.getService(), i'm confused about which to use. I've tried using PendingIntent.FLAG_CANCEL_CURRENT, and UPDATE_CURRENT, neither seem to fix the issue but the result is different, I receive constant INTENT_ALARM_FIRED for every action.
How can I get each pending intent to have its own intent extra data?
Solution
I discovered an exact warning about this scenario right in the PendingIntent doc. http://developer.android.com/reference/android/app/PendingIntent.html
just changed my request codes and it works
This has to do with Intents being considered the same. See PendingIntent for more information.
One way to get around this would be to just vary the requestCode for each Intent. (The 2nd parameter in the getService call)
I'm trying to send SMS messages from an Android App. I'm using PendingIntent to so I can use a Broadcast Receiver to check if it was sent ok. As the sendTextMessage call will be done per SMS, I need to send some "extra" data to identify the actual SMS, so as my broadcast receive can do some work on a specific SMS Message.
Here is my sending code:
String SENT = "SMS_SENT";
Intent sentIntent = new Intent(SENT);
sentIntent.putExtra("foo", "BAR");
PendingIntent sentPI = PendingIntent.getBroadcast(baseContext, 0,
sentIntent, 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(baseContext, 0,
new Intent(DELIVERED), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(smsMessage.getNumber(), null, smsMessage.getText(), sentPI, deliveredPI);
The problem is that in my BroadCast receiver, it can't seem to read my Extra "foo":
public class SMSSentBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent intent) {
String smsID = intent.getStringExtra("foo");
......
}
}
smsID just becomes null.
My broadcast receiver is registered like so:
baseContext.registerReceiver(new SMSSentBroadcastReceiver(), new IntentFilter("SMS_SENT"));
Given that the intent filter is working, what am I doing wrong? Why aren't the extras sent as well?
Any help is appreciated. Thanks
What I suspect has happened (at least, it's happened to me), is that you're using the SMS_SENT intent somewhere else prior, whereby at that time, that intent doesn't have "foo" in the extras. Calling PendingIntent.getBroadcast with the same intent and request code will return you the original pending intent.
If it's actually meant to be the same intent, you need to use something like a PendingIntent.FLAG_UPDATE_CURRENT to update the intent with the new extras. If it's not suppose to be the same intent, it should have a different request code, so that it doesn't match the other intent (which is why feeding in a unique request code worked).
I got a custom status bar notification. There is a "Play" and "Next" image on it.
I want to send a Broadcast Intent to a Service when the "play" or "next" image is pressed.
notification = new Notification(R.drawable.ic_launcher, title + "\n"
+ text, System.currentTimeMillis());
contentIntent = PendingIntent.getActivity(context, 0, actionOnClick, 0);
notification.contentIntent = contentIntent;
contentView = new RemoteViews(context.getPackageName(),
R.layout.custom_player_notification);
contentView.setTextViewText(R.id.custom_player_notification_title,
title);
contentView.setTextViewText(R.id.custom_player_notification_text, text);
notification.contentView = contentView;
This is the code.
The ContentView offers the possibility to set "setOnClickPendingIntent" and "setOnClickFillInIntent". How can I use them to send this broadcast?
I want to send a broadcast because I dont want to launch an activity.
I want to send a Broadcast Intent to a Service when the "play" or "next" image is pressed
I would suggest that you want to send a command via startService() rather than a broadcast.
How can I use them to send this broadcast?
You can try calling setOnClickPendingIntent(), supplying a PendingIntent you create via the static getBroadcast() method on PendingIntent (for a broadcast) or getService() (for a command to be sent via startService()).
Bear in mind that none of this will work on most Android devices available at present. I think buttons-in-Notifications works starting on Android 4.0, perhaps 3.0, but definitely not before then.
You could put something similar to this in your onClick() method of the notification, directing it to a broadcast receiver.
Intent intent = new Intent(this, receiver.class);
intent.putExtra("SomeInfoMaybe", info);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_CANCEL_CURRENT);
And then the code for handling the next stage could go in here:
public class receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
System.out.println("SomeInfoMaybe = " + bundle.getString("info"));
//etc...
}
}
calling .notify(); on your notification should help link to the broadcast receiver
i'm using the code
private void sendSms(String phoneNo, String message){
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, MainScreen.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNo, null, message, pi, null);
}
i have utilized the onSaveInstanceState to save values such as text and spinner position, which will keep things saved and consistent while receiving intents, navigating away from the activity, etc
not sure if i can put extras in the pending intent, so that when it starts my main activity, i can use those extras. because somehow, when the pending intent goes through, i need it to restore to the way it was before the intent was called
You can.. but you must clear the previous pending intent or use the one shot flag while constructing your pending intent.. like this
Intent sentIntent = new Intent(SENT_SMS_ACTION);
sentIntent.putExtra(MESSAGE_ID_TAG, messageId);
PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this, 0, sentIntent, PendingIntent.FLAG_ONE_SHOT);