Android - Manage the notifications - android

I wante to create some notifications to user.
Ex : when a new comment, when someone ask question,...
I know how to create notification, but i don't know how can i do for send a notification just one time.
Example : notification is send to user because he has a new comment. He click on the notification for start application and watch the comment. But now, how can i avoid that it receives twice the same notification ?
It is recommended to store it in a database, in "notification" table or it exist a system for manage this ?
I'm sorry for the very bad English.

Have you looked at Google Cloud Messaging?
https://developers.google.com/cloud-messaging/android/start
If you've already implemented this, and you're receiving the same notification twice, it might have something to do with how you are building the Notification. If the link above doesn't help, can you post more information, and possibly the code?

I solved the problem by checking if the notification is already displayed.
private boolean isNotificationVisible() {
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent test = PendingIntent.getActivity(context, MY_ID, notificationIntent, PendingIntent.FLAG_NO_CREATE);
return test != null;

Related

click event on firebase notification android

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

Android Notifications with customized appearance

As of now, I am using Firebase Cloud Messaging as my app's notification system. From my understanding, even the largest companies use GCM, which is now being replaced with FCM. I am stuck, however, with the limitations imposed by sending things as a "notification" pay load.
Firebase recieves data messages in OnMessageRecieved, and I am able to get my exact desired behavior through using this. However, for the past three months I noticed that my app's notifications were simply not working because data payloads aren't received when the app is in the background/ inactive. This is very frustrating as the only workaround I have found to actually send notifications that alert users is through sending notification payloads.
I don't know how to get my desired behavior with the notification payload as I have been with data payload, but I realize that having notifications display perfectly when the app is in the foreground is pointless if they don't display at all when the app is in the background.
What I'm wondering, and what I'd really appreciate guidance on, is if somebody could recommend a way to display notifications with my desired style rather than the default Firebase notification style. Just to elaborate, I currently have a fully functional system of allowing users to respond to messages through the notification and without opening the app, but only through the data payload.
I know there is a way to somehow send notifications in a customized fashion (adding intents, buttons, etc) when the app is in the background or not running because this is done by Facebook, WhatsApp, and many other apps on the market. I am aware of something called AlarmManagers, but unsure of how I could use them to get my desired behavior, or if this is what people use anyways.
Thank you, and please let me know if there is a way to customize the payload when the app is in the background or inactive, through onMessagesRecieved or any other methods. As of now, the only other solution would be to somehow send the message's contents to a service, but I've faced issues where services could not display notifications in the background without crashing the app and without being very hacky.
The only way I came up with is to send the data payload only, which should be delivered to the onMessageReceived callback despite the app's status (foreground/background).
Then you put all required fields to the data payload and build a notification from it. Here the map "data" is from remoteMessage.getData()
private void sendNotification(Map<String, String> data) {
String body = Optional.ofNullable(data.get("body")).orElse("");
if (body.isEmpty()) {
return;
}
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri sound = getSoundUri(Optional.ofNullable(data.get("sound")).orElse(""));
String defaultChannel = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
? NotificationChannel.DEFAULT_CHANNEL_ID
: DEFAULT_CHANNEL_ID;
String channel = Optional.ofNullable(data.get("android_channel_id")).orElse(defaultChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channel)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(getString(R.string.app_name))
.setContentText(body)
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setShowWhen(true)
.setContentIntent(resultPendingIntent);
if (sound != null)
notificationBuilder.setSound(sound);
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (notificationManager != null)
notificationManager.notify(0, notificationBuilder.build());
}

How to make a notification tap in Android perform multiple actions?

I have set up a notification that opens a website when you tap it. Here's the part of the code that does it.
Intent resultIntent = new Intent(Intent.ACTION_VIEW);
resultIntent.setData(Uri.parse(m.msg.url));
PendingIntent pending = PendingIntent.getActivity(context, 0,
resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notif.setContentIntent(pending);
The notifiaction corresponds to a notification in a website, wich also corresponds to a message in that website. So when you tap the (Android) notification, you go to that website. But I also want to remove the (website) notification by sending a GET request. I already have set a method that does that:
m.delete();
However, I can't find a way to execute both actions at once. The intent should open m.msg.url and execute m.delete(). I have searched for information on Intents and Services but I'm new to Android programming and I don't quite understand how it works. I'd really aprecciate any help or guidance.
Thanks for reading.
Create an Activity or Service that performs the GET then immediately starts the activity you really want to start. Use that as the PendingIntent instead.

Force second activity to refresh (from mainactivity)

i'm a beginner in android , so excuse me for a perhaps stupid question. I'm developing an App with different Features, wich are chosen in the Menu. By google cloud Messaging the App is also receiving push notifications, which are Stored in an MySQL database. These notifications can be shown in a second listactivity. Now my Problem: when this activity is open and a notification is coming in, it is Stored in database, but the aktive listactivity is not updating, Cause it doesn't know. Howe can i Force my listactivity to Refresh from mainactivity when mainactivity is receiving a notificatipn ? Thanks from Germany Fritz
You can post o a notification (for example from a Service) and pass an intent to the corresponding pendingintent.
Intent intent = new Intent(this, MyListActivity.class);
intent.setAction(Intent.ACTION_VIEW);
PendingIntent pi = PendingIntent.getActivity(this, my_code, intent, PendingIntent.FLAG_UPDATE_CURRENT);
...
notificationManager.notify(my_notification);
And in your MyListAcitity you can override the method onNewIntent and reload your data.

Android Instantiate a Dummy PendingIntent

I have an Android library that has a Service that creates a Notification. From what I understand Notification must have a contentIntent (PendingIntent) set or a runtime exception will be thrown.
The problem is that I want users to be able to use this Service as is, or extend it, so that they can set the PendingIntent themselves through a callback during the creation of the Notification. However, if they choose not to do this, I need to set the PendingIntent to something so that there is no exception. Is there any way to create a dummy PendingIntent that just acts as a fill-in?
Here's an example of the code from the createNotification method:
PendingIntent p;
if(getPendingIntentCallback != null) {
p = getPendingIntentCallback.getPendingIntent();
}
else {
p = ?;
}
notification.contentIntent = p;
PendingIntent pi=PendingIntent.getBroadcast(this, 0, new Intent("DoNothing"), 0);
This worked for me. It will launch a broacast for a "DoNothing" action. I hope nobody will listen for a "DoNothing" broadcast and do something as a reaction to it. But if you prefer you can construct something more exotic.
Note: I'm using this just as a temporary placeholder. I will substitute it with something more useful for the user as I will advance in my application development.
A notification needs to have some sort of action associated with it. You have to tell android what to do when a app user clicks the notification. Letting your library fail if no contentIntent has been defined will let your library user know that they have missed a very important step.
You could check for the pending intent before creating your notification.
if(getPendingIntentCallback != null) {
p = getPendingIntentCallback.getPendingIntent();
// create a notification
}
else {
//don't create a notification
Log.d("Notification", "Your notification was not created because no pending intent was found");
}
Or to answer the question you asked, you could create a dummy pending intent that performs some arbitrary action like going to the home screen.
See this thread:
How to Launch Home Screen Programmatically in Android

Categories

Resources