How to Send Parameters from a Notification-Click to an Activity

How to send parameters from a notification-click to an activity?

Take a look at this guide (creating a notification) and to samples ApiDemos "StatusBarNotifications" and "NotificationDisplay".

For managing if the activity is already running you have two ways:

  1. Add FLAG_ACTIVITY_SINGLE_TOP flag to the Intent when launching the activity, and then in the activity class implement onNewIntent(Intent intent) event handler, that way you can access the new intent that was called for the activity (which is not the same as just calling getIntent(), this will always return the first Intent that launched your activity.

  2. Same as number one, but instead of adding a flag to the Intent you must add "singleTop" in your activity AndroidManifest.xml.

If you use intent extras, remeber to call PendingIntent.getActivity() with the flag PendingIntent.FLAG_UPDATE_CURRENT, otherwise the same extras will be reused for every notification.

Send data to activity when Firebase notification is clicked in Android

When sending notification from console, add the custom data from 'Advanced options':

Sample Image

For some reason, firebase doesn't allow the key to start with fcm. You cannot use fcm_notification. Use a different key.

For above image, receive the key as follows:

String value = getIntent().getStringExtra("test_key");

How to send data from Notification with click Notification android

The problem is with the way you are getting the intent in the MainActivityFargmainMarket class.

Here's what you need to do:

1 - Set activityIntent flag to FLAG_ACTIVITY_SINGLE_TOP, which you already made.

2 - Set contentIntent flag to PendingIntent.FLAG_UPDATE_CURRENT, if not it will reuse the same extras for every notification.

3 - Override the onNewIntent method in the MainActivityFargmainMarket class to get the activityIntent you sent

Private Intent intent;
@Override
protected void onNewIntent(Intent _intent){
super.onNewIntent(_intent);
if(_intent == null)
Log.i(TAG, "_intent is null");
else {
intent = _intent;
getData();
}
private void getData(){
String extraBody = intent.getStringExtra("key");
Log.i(TAG, "ddddfg"+extraBody.toString());
}

do not try to get any data from intent before checking if null

Sending parameters from a notification to a different activity?

I was lead to an answer via this question: FLAG_ACTIVITY_NEW_TASK not behaving as expected when used in PendingIntent

First, I changed this line:

Intent notificationIntent = new Intent( this, MainActivity.class );

To:

Intent notificationIntent = new Intent( this, NotificationActivity.class );

I made NotificationActivity.class a placeholder activity that will let other activities know when the app is being resumed from a notification. These lines of code were removed:

notificationIntent.setAction( Intent.ACTION_MAIN );
notificationIntent.addCategory( Intent.CATEGORY_LAUNCHER );
notificationIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED );
notificationIntent.putExtra( NotifyService.NOTIFICATION_INTENT, true );

And, this line:

PendingIntent contentIntent = PendingIntent.getActivity( this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT );

Was changed to:

PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT );

In the onCreate() method of NotificationActivity, I added these lines to the bottom:

STARTED_FROM_NOTIFICATION = true;
finish();

Where STARTED_FROM_NOTIFICATION is in a common source file. Then, in each activity in the onStart() method:

if( STARTED_FROM_NOTIFICATION == true )
{
STARTED_FROM_NOTIFICATION = false;
// Do something
}

I also removed android:launchMode="singleTop" from all activities.

Using this method, the NotificationActivity will be placed on top of the existing stack of activities, but will be finished before it is displayed. After it is finished, onStart() for the activity below it will be called and operation works as desired.

Send params activity when use Parameters for notification messaging by platform GCM (android)

You can use this one to send notification

private void sendNotification(String from, String message) {
Bundle bundle = new Bundle();
bundle.putString("name", from);
bundle.putString("message", message);
Intent intent = new Intent(this, ChatActivity.class);
intent.putExtra("INFO", bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle(from)
.setSound(defaultSoundUri)
.setContentText(message)
.setSmallIcon(R.drawable.ic_notification)
.setAutoCancel(true)
.setContentIntent(pendingIntent);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0, builder.build());
}

And don't forget to add WAKE_LOCK permission in Manifest file:

<uses-permission android:name="android.permission.WAKE_LOCK" />


Related Topics



Leave a reply



Submit