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.
Related
about event for click on notification:
I am searching since yesterday about that , what I found/understood: in FirebaseMessagingService will receive notification data , after will fire local notification, so need to add event in that local notification, I tried to add that many times with many ways but nothing worked …
after I tried to deleted notification files (firebase notification files, and local notification files) but still can receive notification. do you know how to know if the user clicked on the notification ?
To receive messages, use a service that extends FirebaseMessagingService. Your service should override the onMessageReceived and onDeletedMessages callbacks. It should handle any message within 20 seconds of receipt (10 seconds on Android Marshmallow). The time window may be shorter depending on OS delays incurred ahead of calling onMessageReceived. After that time, various OS behaviors such as Android O's background execution limits may interfere with your ability to complete your work.
For further info. you can visit the official website:
Link: https://firebase.google.com/docs/cloud-messaging/android/receive
Hope you'll get your answer here.
Step 1:
// Create an Intent for the activity you want to start
Intent intent = new Intent(this, MainActivity.class);
Step 2:
// Create the PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(this, Calendar.getInstance().get(Calendar.MILLISECOND), intent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
Step3:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentIntent(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
Whenever a user clicks on notification MainActivity will be opened.
Here is details implementation of Android Notification Sample https://github.com/android/user-interface-samples/tree/master/Notifications
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.
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.
I am trying to implement local notifications on an android application that I am building. What I want to do is to have a local notification once a week, and when the user presses the notification then a new activity will appear with the actual notification text.
I tried the tutorial below but with no luck:
http://karanbalkar.com/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/
the notifications is not repeating every week and when i press it the first time that it appears, then it does not open the new activity.
Anyone who can give me a better tutorial or a good solution?
I don't know exactly how to set up your weekly notifications but i know how to start Activity when user click on Notification and clear the notification list after it:
There is declaration of Notification manager, and notification builder:
NotificationManager notificationManager = (NotificationManager)
Context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
Uri alarmSound = RingtoneManager.getDefaultUri(R.raw.ok);
notificationBuilder.setSound(alarmSound);
//There is declaration which Activity will be opened if user click on notification:
Intent notificationIntent = new Intent(context, Notifications.class);
//clear notification after clicking on it:
notificationBuilder.setAutoCancel(true);
PendingIntent newIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notificationBuilder.setContentIntent(newIntent);
notificationBuilder.setSmallIcon(R.drawable.ic_launcher).setContentTitle("APP TITLE").setContentText("New notification");
notificationManager.notify(001, notificationBuilder.build());
I hope this will help you!
I don't have any experience with notifications and after a long time now, I'm feeling really stupid, that I can't make it:
I'm getting push notifications from my server. On click of one, I'm getting the inormations with
getIntent().getBundleExtra("extra");
in the onResume method.
But if I'm getting a lot of notifications and don't open my application between this, then I'm just getting the last, if I click on the notification. Where are the notifications before? The best would be, if I'm getting a list of bundles or something like this. How can I do that?
Thanks for help, I'm really depressed :)
EDIT:
For better understanding: After receive a GCM I'm make a notification like this:
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, title, System.currentTimeMillis());
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
if (extras != null && launchIntent != null) {
launchIntent.putExtra("extra", extras);
}
PendingIntent intent = PendingIntent.getActivity(context, -1, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, msg, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(0, notification);
Example: I get a GCM and set a notification. If I don't open my application, but get the next GCM, then my first notification will replaced by the new one. This could be many times. After a few notifications i click on it. If the app starts I will get all the notifications, which has been sent. At the moment I just get the last one. How can I get all?
mNotificationManager.notify(0/* 0 is your notification id */, notification);
creates a notification with id 0. Now when you receive another notification and you notify it with the same notificaionId (i.e 0). Hence the latter pending Intent replaces the previous pendingIntent.
Check the explanation for notify() in the docs =>
http://developer.android.com/reference/android/app/NotificationManager.html
The best option would be to save your data locally in some DB, preferences or the traditional way
File and whenever you receive a push notification from GCM save it locally and then notify.