I am using baidu push notification in my app. The issues i am facing are i am not able to customize its notification UI and not able to clear it from status bar unless i tap on that. I could see below code in theirs demo project to customize the UI. But it does not make any difference. please help me.
CustomPushNotificationBuilder cBuilder = new CustomPushNotificationBuilder(
getApplicationContext(), R.layout.notification_custom_builder,
R.id.notification_icon, R.id.notification_title,
R.id.notification_text);
cBuilder.setNotificationFlags(Notification.FLAG_AUTO_CANCEL);
cBuilder.setNotificationDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE);
cBuilder.setStatusbarIcon(getApplicationContext().getApplicationInfo().icon);
cBuilder.setNotificationTitle("push");
cBuilder.setNotificationText("baidu");
PushManager.setNotificationBuilder(getApplicationContext(), 1, cBuilder);
I got answer for this question. Actually the second parameter i pass in PushManager.setNotificationBuilder(getApplicationContext(), 1, cBuilder); is notification ID. Means if u want to display a custom UI from server this id should be passed with notification. Then client will take corresponding builder with that ID. In my case i was not getting this parameter from server
you can pass setNotificationTitle("notification_title"); and setNotificationText("notification_content"); this two parameter is fixed from baidu push notification server
Related
This push notification is used to notify when a new chat message is available. I want to do like Instagram, that is to say, the message into this notification is updated at each new income message received, until the user open the chat.
So, how can I retrieve the message for this specific notification? Is it possible, or I have to save (in cache) the latest messages... ?
Thank you very much
I suggest saving messages in the database, and then in the function that sends notifications, get messages (probably recent unread ones - I don't know what your logic looks like in this case).
If there are more than one, use InboxStyle():
val otherMessagesToShow = getMostRecentUnreadMessages()
if(otherMessagesToShow.size > 1) {
val style = NotificationCompat.InboxStyle()
otherMessagesToShow.forEach { style.addLine(it) }
builder.setStyle(style)
builder.setNumber(otherMessagesToShow.size)
}
Finally, update the existing one (passing the same notificationID):
notificationManager.notify(notificationId, builder.build())
I have made an android app (Android Studio / Java) that checks a website for content and stores it in an sqlite DB. If new content is fetched that is not stored in DB, it shows a notification with the new content for user to notice.
That's working fine, although if user does not read/open/dismiss that notification, the next notification will update current one and replace its content with new data. This is wanted behavior, because I don't want the user to receive many notifications for the same thing, so I'm using the same notification id.
This introduces a problem though, if user checks the notification now, he will see the second fetched data, but won't be aware of the existence of the first fetched data.
So, what I'm trying to do is to append to the notification's content, so that both first and second fetched data are shown.
I tried the "inboxStyle" notifications that allow for new lines to be added, but it seems to be working only for setting many lines at the time notification is created and not for appending lines to an existing notifications.
I know that I can do that by storing what user has seen and what not, whether a notification was opened, etc, but this seems too much hassle for a simple thing, there must be an easier way to achieve it.
The expected behavior would be to either be able to append the message of existing notifications, or be able to fetch the message of an existing notification (by id) and then manually append to it and push the updated notification.
If that's not clear enough, the expected outcome is:
Issue the first notification with message "Test message 1"
Issue second notification using the same notification-id with message "Test message 2" that would NOT overwrite "Test message 1" but rather keep that message and append to it, so that the notification's message would now be "Test message 1 {newline-here} Test message 2" (or even better reversed so that the last message is shown on top).
Thank you in advance!
I was looking for a solution myself. I managed to do something that works but I'm quite sure there are other elegant solutions : I'm checking whether or not similar notification has been displayed. If so, I get the previous content and append it to the new notification content before publishing.
First, make sure that the notifications you want to assemble have the same uniqueID
This way, when you receive your 2nd, 3rd notification, you can easily match the one you are creating with the ones already displayed.
Note : This code works only on API 23 and higher.
String message = null;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications();
for (StatusBarNotification notification: notifications) {
if (notification.getId() == uniqueID) {
// You have a match
Bundle extras = notification.getNotification().extras;
message = extras.getCharSequence(Notification.EXTRA_TEXT).toString();
break;
}
}
}
The easy part : Now that you have the previous notification text, you have to append it to the new notification text before publishing
String newMessage = message + remoteMessage.getData.get("text");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(),
channelID)
.setStyle(new NotificationCompat.BigTextStyle().bigText(newMessage))
.setContentTitle("Title")
.setContentText(notification.getMessage())
.setAutoCancel(true);
I am using a rest API to send Baidu Push notifications. I don't have access to Android's notification builder classes, so I can't, for example, use notificationBuilder.setPriority(2);. Really the main thing that I need to know is how to set the priority of a Baidu Push notification to MAX, using JSON only.
According to this website, here are all of the keys and values for a Baidu JSON payload:
{
//android必选,ios可选
"title": "hello" ,
"description": "hello world"
//android特有字段,可选
"notification_builder_id": 0,
"notification_basic_style": 7,
"open_type":0,
"net_support" : 1,
"user_confirm": 0,
"url": "http://developer.baidu.com",
"pkg_content":"",
"pkg_name" : "com.baidu.bccsclient",
"pkg_version":"0.1",
//android自定义字段
"custom_content": {
"key1":"value1",
"key2":"value2"
},
//ios特有字段,可选
"aps": {
"alert":"Message From Baidu Push",
"sound":"",
"badge":0
},
//ios的自定义字段
"key1":"value1",
"key2":"value2"
}
I assume I need to set the values of notification_builder_id, but as it simply takes an integer as a the value, I am not sure how to create the notification to correspond with the id.
I was able to find out how to set the priority to high, in order to make the notification appear as a banner, as opposed to just an icon in the status bar.
In the client app, you need to set the notification builder, and give it an id, like so (note that I'm using Xamarin Forms, but the logic is the same if you are using native Android. Also, this code should be placed directly after the PushManager.StartWork() method call, most likely in your activity's onCreate() method):
BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder();
builder.SetNotificationFlags(
(int)NotificationFlags.AutoCancel |
(int)NotificationFlags.HighPriority |
(int)NotificationFlags.ShowLights);
builder.SetNotificationDefaults(
(int)NotificationDefaults.Lights |
(int)NotificationDefaults.Vibrate);
builder.SetStatusbarIcon(Resource.Drawable.icon);
// the second parameter is the id. This is the id that you need to set
// in the JSON payload in order for the incoming notification to appear
// in this way.
PushManager.SetNotificationBuilder(Xamarin.Forms.Forms.Context, 1, builder);
And then for the payload, its simply like this:
{"title":"My Title","description":"my description","notification_builder_id":1}
Again, note that the value of notification_builder_id has to correspond with the second parameter in the SetNotificationBuilder() method.
Tried to show 3 notification in cluster format. As per the doc, I added the setGroupSummary(true) property for the first notification.But in the result i have got only two notification. The notification which is added the GroupSummary property is not visible.
NotificationCompat.Builder firstNotification = createNotification(context,"1.Message","Here you go 1");
firstNotification .setGroupSummary(true);
firstNotification .setGroup("KEY_NOTIFICATION_GROUP");
NotificationCompat.Builder secondNotifi = createNotification(context,"2.Message","Here you go 2");
secondNotifi .setGroup("KEY_NOTIFICATION_GROUP");
NotificationCompat.Builder thirdNotifi= createNotification(context,"3.Message","Here you go 3");
thirdNotifi.setGroup("KEY_NOTIFICATION_GROUP");
Here the notification trigger,
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,firstNotification .build());
notificationManager.notify(1,secondNotifi .build());
notificationManager.notify(2,thirdNotifi.build());
And the result is,
I want to show all three notification in the cluster format without missing.
Any help will be really appreciated.
You should check the following answer :
setgroup() in notification not working
You have to create a separate group notification and set the group summary flag true only for that, and that becomes the parent notification that bundles other notifications with the same group key within itself.
setGroupSummary's purpose is to support API levels below Nougat. On Android 7.0 and higher, it shows a normal group and just uses the on click behavior (setContentIntent) and details like the summary text of the summary notification.
On Android 7.0 and lower, it shows your summary notification as a replacement for all the other notifications the group contains.
Android 7 makes a decision regarding summary notification is shown by itself. So, you want see it unless system decides that it needs to be displayed.
Solution: create a dedicated summary notification.
On Android L, I would like show the user a notification on the lock screen only if the user settings is set to "show all notification content", otherwise the content will be pointless and I just prefer not to show the notification at all.
Any idea how to verify in code the user notification settings?
Thanks!
You need to read
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS = "lock_screen_allow_private_notifications"
Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS = "lock_screen_show_notifications"
only if both are 1 then you need to show your notifications.
But since these values are not part of public api, these might change in future, or might not work on all devices
int show_all = Settings.Secure.getInt(getContentResolver(),"lock_screen_allow_private_notifications", -1);
int noti_enabled = Settings.Secure.getInt(getContentResolver(),"lock_screen_show_notifications", -1);
if(show_all > 0 && noti_enabled > 0){
//post noti
}
You can't check that setting as far as I know, but your app can control the level of detail visible when its notifications are displayed over the secure lock screen. To control the visibility level, call setVisibility() (Notification.Builder.setVisibility) and specify one of these values:
VISIBILITY_PUBLIC: Shows the notification’s full content.
VISIBILITY_PRIVATE: Shows basic information, such as the notification’s icon, but hides the notification’s full content.
VISIBILITY_SECRET: Shows nothing, excluding even the notification’s icon.
When the visibility level is VISIBILITY_PRIVATE, you can also provide a redacted version of the notification content that hides personal details. For example, an SMS app might display a notification that shows "You have 3 new text messages" but hides the message content and senders. To provide this alternative notification, first create the replacement notification using Notification.Builder. When you create the private notification object, attach the replacement notification to it through the setPublicVersion() method.
Sources