Clicking on Notification Is Not Starting Intended Activity

Clicking on Notification is not starting intended activity?

My problem got solved I just have to add PendingIntent.FLAG_ONE_SHOT flag as well , so I replaced :

PendingIntent contentIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

to

PendingIntent contentIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);

Notification is not opening Activity onClick

First you need to declare an Intent like:

Intent notificationClickIntent = new Intent("intent_id")

Then you need to declare a Pending Intent like this:

PendingIntent pendingIntent = PendingIntent.getActivity(context, REQUEST_CODE, notificationClickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

In your notification builder add set contentIntent:

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(notificationData.getTitle())
.setContentText(notificationData.getBody())
.setPriority(NotificationCompat.PRIORITY_HIGH)
**.setContentIntent(notificationClickPendingIntent)**
.addAction(R.drawable.ic_notification, context.getString(R.string.turn_on_alarms), notificationButtonClickPendingIntent)
.setAutoCancel(true)

In your manifest declare an intent filter on the activity you want to open (as I remember I could not make it work for the launcher activity, maybe it can work, I just gave up).

<activity
android:name="Activity_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar.LightStatusBar">
<intent-filter>
<action android:name="intent_id" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

And last but not least, if activity hasn't been opened, notification onClick will trigger the onCreate method, if it has been opened, it will trigger the onNewIntent

hope this helps :)

android How to start activity when user clicks a notification?

Here is my final solution:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(YourService.this)
.setContentTitle(getResources().getText(R.string.app_name))
.setContentText(getServiceStateDescription(HomeBridgeService.this))
.setSmallIcon(iconId)
.setWhen(System.currentTimeMillis());

Intent nIntent = getPreviousIntent();
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(MainActivity_.class);
stackBuilder.addNextIntent(nIntent);
PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);

startForeground(ContextConstants.LAUNCHER_SERVICE_NOTE_ID, notificationBuilder.build());

private Intent getPreviousIntent() {
Intent newIntent = null;
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final List<ActivityManager.AppTask> recentTaskInfos = activityManager.getAppTasks();
if (!recentTaskInfos.isEmpty()) {
for (ActivityManager.AppTask appTaskTaskInfo: recentTaskInfos) {
if (appTaskTaskInfo.getTaskInfo().baseIntent.getComponent().getPackageName().equals(ContextConstants.PACKAGE_NAME)) {
newIntent = appTaskTaskInfo.getTaskInfo().baseIntent;
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
}
}
} else {
final List<ActivityManager.RecentTaskInfo> recentTaskInfos = activityManager.getRecentTasks(1024, 0);
if (!recentTaskInfos.isEmpty()) {
for (ActivityManager.RecentTaskInfo recentTaskInfo: recentTaskInfos) {
if (recentTaskInfo.baseIntent.getComponent().getPackageName().equals(ContextConstants.PACKAGE_NAME)) {
newIntent = recentTaskInfo.baseIntent;
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
}
}
}
if (newIntent == null) newIntent = new Intent();
return newIntent;
}

FCM notification not opening intended activity Android

Extending #Sudip Podder comments and #Ratilal Chopda answer
Follow these steps:

Step1:

<activity android:name=".SplashActivity">
<intent-filter>
<action android:name=".SplashActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Step2:

I am using php at server so you need to adjust things the way you like but in notification payload add "click_action" : ".SplashActivity"

 $fields = array(
'to' => $token,
'notification' => array(
'title' => 'Motors City',
'body' => $message,
"click_action" => ".AppSplash",

),
'data' => array(
'sec_id' => $secID,
'sec' => $sec,
'extra1'=>$extra1,
'extra2'=>$extra2
)
);

$headers = array(
'Authorization:key=' . $server_key,
'Content-Type:application/json'
);

Step3:
In Oncreate of your SplashActivity

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
Log.d(TAG,bundle.toString);
}}

and you are done

FCM not opening activity again when app opened by clicking notification

OP's solution:

i.setPackage(null);
i.setFlags(Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_RECEIVER_NO_ABORT );
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT |
PendingIntent.FLAG_ONE_SHOT |
PendingIntent.FLAG_IMMUTABLE);


Related Topics



Leave a reply



Submit