Android app doesn't open required activity instead it opens other - android

Help me out friends I have been facing a problem from a week
I have made an android app in which I got 3 activities named
MainActivity
NotificationListActivity
NotificationActivity
other than that I have 2 services in my app ( as because I am using FCM )
JLZFirebaseMsgService
JLZInstanceIdService
in JLZFirebaseMsgService.java I have written the code
private void sendNotification(String messageBody) {
Intent intent = new Intent(this,NotificationActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Jee Lo Zindagi 2.0")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
what I want is to whenever FCM server sends the notification to the devices the app should open NotificationActivity as the I wrote
but whenever I tap on the notification it opens the MainActivity I don't know why.
when the app is open the notification comes and by tapping on it we go to the NotificationActivity that's what I wanted but not the same scenario when the app is closed.
but sometimes it happens that the app shows the correct activity by tapping on the notification I still don't know why.

You should use "data" insted of "notification" on your server code
When you use "notification" on your server code
while you app is not running the JLZFirebaseMsgService.java does not start . instead the you will recevie a notification with your launcher icon
But if you use "data" on your server code your messaging service will start

It seems that you are sending a notification messages, these are messages that go to onMessageReceived when your app is in the foreground in your case that will result in you manually generating a notification, when your app is in the background it will result in an auto-generated notification that your code does not define.
If you are using the FCM REST API then you can use the click_action field which would allow you to define which activity would be launched when the user taps on the notification. If you are using the Firebase console there currently you cannot specify a click_action from there.

Related

clicking on group of notification is not redirecting to specific screen, instead of that app is restarted in oreo

I have implemented firebasse notification functionality in my chat app and everything is running fine but now while testing in Android os 8.0 and 8.1 when app is in background and if user is getting 4 or more than 4 notification then its combined in group and when user click on group then not getting intent and app is restarted.
If user tap on single notification then I am able to send him in specific screen.
I want Notification data or chat id so I can send him in specific screen but not getting any data in intent.
I have searched similar kind of question in stackoverflow but still not getting proper result.
Android: Clicking Grouped Notifications Restarts App
How to open non-launcher activity on notification group click
You can use setContentIntent to set the value of Intent like below
Intent intent = new Intent(this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.your_notification_icon)
.setContentTitle("Notification Title")
.setContentText("Notification ")
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
It will open SecondActivity (Replace the SecondActivity with your own activity)
I am having the same issue causing app restart in Oreo, after many tries i noticed that if when creating the notification i call .setGroup(string) the system no longer groups the notifications.I am creating the notifications from a service. This is not a solution i know but not grouping is a lesser evil than app restart.

Can't start Application to particular activity on Notification click from FCM

I'm experiencing this problem: If I click on the notification and my app is closed, HomeActivity is opened instead of the requested one.
I'm raising the notification like this:
Intent intent = new Intent(context, ArticleActivity_.class);
intent.putExtra(ArticleActivity.KEY_ARTICLE_ID, articleId);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 , intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(notification.getTitle())
.setContentText(notification.getBody())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
Notification n = notificationBuilder.build();
n.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 , n);
And everything works fine if the application is opened. But if I close it, after clicking on the notification, HomeActivity will be opened instead of the one I configured.
You are having this problem because that is how FCM works. There are 2 types of push notifications. The first type is "notification" and the second type is "data". Both can be visualize as a JSON payload:
"notification": {
//key values
}
"data": {
//your custom key values
}
There is a third type which is a combination of both.
The "notification" type will create a default visual notification with default behavior when the app is not in the foreground. And it will execute whatever your write inside onMessageReceived when the app is open. The second type "data" will always do what you code inside onMessageReceived.
From the Firebase web console, you can send "notification" type or the combined type. For sending only "data" you need to send it from Firebase Functions or from a Server (that complies with the FCM requisites).
There is a trick you can do to get your goal. Combined payload, that can be send from the Firebase web console, contains "notification" and "data". The "data" key-values are available after the user click the notification. That information is available as extra data from the intent opening the default launcher Activity, so there you can change your Activity.
In your launcher Activity, add the following:
String notification = getIntent().getStringExtra("your-key");
if (notification != null) {
startActivity(new Intent(this, YourActivity.class));
}
This way you can validate that the Activity is being opened because the default notification was clicked, and is always a String because payload in push FCM must be by rule always String. In other cases, if the user simply opened the app, then the String will be null, and you don't redirect the user.

I want to send information through notification

I am making an app for my university, now in which I want to send messages/information about events. How I can send info to them. I tried firebase but, it is working till push notification only. I want if someone clicks on the notification it should open a new activity with whole message/info any code reference will be very helpful.
use the payload to put information into your notification. this could be an event id. The text of your notification is is also set by your server.
Thanks to the payload, your app client knows what event ID was received with the notification (when notification was opened). Just ask your database about information from the received event id from the client and you have all you need.
public void getnotification() {
NotificationManager notificationmgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this, your_acitivity.class);
PendingIntent pintent = PendingIntent.getActivities(this, (int) System.currentTimeMillis(), new Intent[]{intent}, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notif = new Notification.Builder(this)
.setSmallIcon(R.drawable.shadow_fiend)
.setContentTitle(notif_title)//Title of the notification
.setContentText(notif_detail)//Description of the notification
.setContentIntent(pintent)
.setAutoCancel(true)
.build();
notificationmgr.notify(0, notif);
}
When the above function is called a notification is generated and by clicking the notification the user is redirected to a specific activity. In the intent replace the your_acitivity.class to whatever activity you want to be redirected when notification is clicked.
Feel free to comment if I misunderstood your requirements.

How to handle the case when click_action was sent to the previous version of the app where intent-filter was not added yet?

Could you help me to solve the problem?
The task is develop push notification that will open particular Activity by users tap.
I've added intent-filter and tried to send the push message with the click_action field, and everything works like a charm.
The problem is with the previous version of the application where Intent-filter was not added yet. If user tap on the push message (in the case of previous app version), nothing happens.
What is better to do in this case? How to handle such situations?
Edit:
So the question is, how can we have default behavior (opening the app on the last screen) in the case of specific screen mentioned in click_action field and lacking of necessary intent-filter? This relevant for situation when the user has previous version of the app without necessary intent-filter in the manifest file.
To perform some action on push-notification click we need to write code for that to perform such action. In your case you want to perform some action on your old live application, In this can you can't do anything.
To perform some action you can call this method on push notification received
private void sendNotification(String title, String messageBody) {
Intent intent = new Intent(this, YourActivityName.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(messageBody)
.setVibrate(new long[]{01})
.setAutoCancel(true)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
notificationBuilder.setSmallIcon(R.mipmap.notification);
else
notificationBuilder.setSmallIcon(R.mipmap.notification);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notificationBuilder.build());
}

Notification using setFullScreenIntent() for BigTextStyle opening Activity automatically

I have a very strange issue, I am working on Push Notification and it was successfully implemented but when i have used BigTextStyle in Notification to show a long message in notification area with setFullScreenIntent() method then the issue coming up the Notification opening the Activity automatically which is set in PendingIntent.
If I don't use setFullScreenIntent() then notification won't opening Activity automatically the user has to tap or click on Notification to open the Activity set in PendingIntent.
So there are two codes
Without setFullScreenIntent() working fine and not opening Activity automatically:
notification = new NotificationCompat.Builder(context)
.setContentTitle("Title")
.setContentIntent(resultPendingIntent)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification.build());
With setFullScreenIntent() also working fine but opening Activity automatically:-
notification = new NotificationCompat.Builder(context)
.setContentTitle("Title")
.setContentIntent(resultPendingIntent)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)
.setFullScreenIntent(resultPendingIntent, true) //Whether true or false same result
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification.build());
public NotificationCompat.Builder setFullScreenIntent (PendingIntent intent, boolean highPriority)
An intent to launch instead of posting the notification to the status
bar. Only for use with extremely high-priority notifications demanding
the user's immediate attention, such as an incoming phone call or
alarm clock that the user has explicitly set to a particular time. If
this facility is used for something else, please give the user an
option to turn it off and use a normal notification, as this can be
extremely disruptive.
On some platforms, the system UI may choose to display a heads-up
notification, instead of launching this intent, while the user is
using the device.
Parameters
intent: The pending intent to launch.
highPriority: Passing
true will cause this notification to be sent even if other
notifications are suppressed.
Found here. As you can see it immediately launches the intent. I don't really know in what case you wanted to use setFullScreenIntent()?
A notification won't automatically expand when a static notification is displayed on top (could be custom bar with wifi, bluetooth and sound control)
pass setFullScreenIntent and setContentIntent with different pending intents.
Worked for me. click on Notif will work and autolaunch will stop

Categories

Resources