What methods are called when opening app from push notification - android

I'm trying to figure out what methods are called when my app is opened by tapping on a push-notification banner. The app appears to re-launch?

you can use pending Intent, like this:
val intent = Intent(this#MyFirebaseMessagingService, NextActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 101, intent,
PendingIntent.FLAG_UPDATE_CURRENT)
and set pendingIntent to your notification.Builder.

Related

Multiple Notification with dismiss notification action button

I have an app which associates some entities with a unique ID and notifies about the entities to the user, which I'm gonna use notificationID to be the same as entity ID.
I have built a notification with a dismiss action based on the following sample solution exactly without any modification.
So far things are going well until I try to create 2 notifications with different ID using the sample.
A problem arises in that the dismiss button only receives the notificationID of the first notification:
The first notification behaves normally as expected.
But the second notification's getExtra() in BroadcastReceiver takes the notificationID of the FIRST notification instead and cancelling the notification just keeps cancelling the first notification.
My create Notification function, I just call this function twice with different IDs:
void createNoti(int NOTIFICATION_ID){
Intent buttonIntent = new Intent(context, ButtonReceiver.class);
buttonIntent.putExtra("notificationId", NOTIFICATION_ID);
PendingIntent btPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, buttonIntent, 0);
NotificationCompat.Builder mb = new NotificationCompat.Builder(getBaseContext());
mb.addAction(R.drawable.ic_Action, "My Action", btPendingIntent);
manager.notify(NOTIFICATION_ID, mb.build());
}
BroadcastReceiver class:
public class ButtonReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int notificationId = intent.getIntExtra("notificationId", 0);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(notificationId);
}
}
I believe the issue is in passing in 0 into PendingIntent:
PendingIntent btPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, buttonIntent, 0);
I had the same issue until I started passing in the notification id as the 2nd argument; so instead of passing in 0, pass in the id of the notification:
PendingIntent btPendingIntent = PendingIntent.getActivity(getApplicationContext(), NOTIFICATION_ID, buttonIntent, 0);
After I made that change, I noticed that when clicking on individual notifications (especially notifications in a group) everything worked as intended.

Pending Intent for more than one notification

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)

Parcelable content not updating

I have a JSON message being sent to a listening service in my first Android app (read: pelase be gentle!) that is mapped to an object that implements Parcelable. The deserialized object is used to display a notification, with an intent that is meant to launch another activity displaying the full data in a layout.
Using the deserialized object I'm able to display the information in the notification without issue. The code in the notification to launch the secondary activity:
Intent intent = new Intent(this, SecondaryActivity.class);
intent.putExtra("objData", myObj);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle(myObj.getTitle())
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(myObj.getAbstract()))
.setContentText(myObj.getAbstract());
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
In the SecondaryActivity I unwrap the parcel like so:
MyObject myObj = getIntent().getParcelableExtra("objData");
and use its data to fill in areas of the layout.
This works great on the first notification. On subsequent notifications, the notification looks correct but the parcelable data sent to SecondaryActivity is not updated (the content appears to the same as the first notification).
I'm assuming I'm missing something obvious that is preventing the parcelable object from being updated. Can you help?
When creating the pending intent you could try adding the flag FLAG_CANCEL_CURRENT or FLAG_UPDATE_CURRENT
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

phonegap local notification

i have implemented a local notification in my android app using phonegap
localnotification plugin.
notification get trigger properly but while clicking on notification
app does not open.
what i want to relaunch a app on clicking of status notification.
Thanks and regards
In AlarmReciever class, in onRecieve event, change the line 79,
final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
to
final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context,mainActivity.class), 0);
It starts, new activity

Android - Track Notification onclicked

I want to start a activity when users clicked on notification but there are no onclicked event for notification (?), so how can we know when the notification is clicked?
Thanks!
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Look into PendingIntent setup there.
The PendingIntent will get called when you click the notification.
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notificationIntent will be called when notification is clicked.

Categories

Resources