I have an app with media buttons and text in the notification that I want to update programatically. So I did this:
public void updateNotificatonIcons() {
ArrayList<NotificationCompat.Action> acts = NotificationBuilder.mActions;
acts.set(1, new NotificationCompat.Action((isPlaying) ? R.drawable.my_media_stop : R.drawable.my_media_play, "Stop", stopintent));
NotificationBuilder.mActions = acts;
NotificationManagerCompat.from(context).notify(1, NotificationBuilder.build());
}
The problem is that AndroidStudio complains about NotificationBuilder.mActions:
Builder.mActions can only be accessed from within the same library group prefix (referenced groupId=androidx.core with prefix androidx from groupId=Android)
I googled and added a #SuppressLint("RestrictedApi") before the method, and it works.
Is this the "correct" way of updating the notification? Is there a better way without the need of a #SupressLint?
I'm using PlayerNotificationManager for displaying playback notifications in my application. Everything is working fine but I want to add my application's logo in the notification with custom fonts.
I changed the play & pause buttons on the notification by adding drawables.xml. But can't find a way to change the font.
So, how can I change the default notification layout and characteristics that Exoplayer provides?
Edit:
I have seen this issue on Github which says that we need to extend PlayerNotificationManager in order to use custom layout. But I can't seem to get it working.
Here is my code:
private void setPlayerNotificationManager(Player player) {
playerNotificationManager = new PlayerNotificationManager(context, "123", 1234, mediaDescriptionAdapter);
playerNotificationManager.setUseNavigationActions(false);
playerNotificationManager.setUsePlayPauseActions(true);
playerNotificationManager.setRewindIncrementMs(0);
playerNotificationManager.setFastForwardIncrementMs(0);
//playerNotificationManager.setColor(Color.GREEN);
playerNotificationManager.setColorized(true);
playerNotificationManager.setUseChronometer(false);
playerNotificationManager.setSmallIcon(R.drawable.logo);
playerNotificationManager.setPlayer(player);
}
As said on the repo you need to extends the PlayerNotificationManager to do that you need to create this class and copy everything inside your project, and then add the layout you want to use as described in this link
You need to add your layout in the builder here
I want my FCM notifications to include a custom image and a vibration pattern.
The AndroidNotification class that Firebase provides as an example has both of these variables, but I haven't seen any way to get it working.
I've tried adding both of these variables to my notification message through a c# class AndroidNotification which gets serialized to json.
This is the documentation that makes me think it should be possible
https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
I have watched multiple online videos and went through multiple forms, but have still been unable to set a custom vibration pattern or display image along with my app icon.
Lots of places suggest that I use a data only message so I can handle displaying the notification myself, but if that is the only way to do it way does the AndroidNotification documentation(linked above) show fields for both vibrate_timings and image?
Note :
I've been testing on Android Versions 4, 5, and 8. On 8 I set up a notification channel and everything works fine with that including the vibrations, but still no image.
The image is included locally in the my apk at the same path as my custom icon.
Also my apk is built with unity but I don't think that should effect things.
Here is the json payload I'm sending, I get my custom sound, custom icon, custom color etc just fine, it's only the image and vibrate_timings that don't seem to be working.
{
"validate_only":false,
"message":{
"data":null,
"android":{
"collapse_key":"new_messages",
"priority":1,
"restricted_package_name":"",
"data":{
},
"notification":{
"title":"A spoon is ready!",
"body":"Grab it before someone else!",
"icon":"spoonsbuzz",
"channel_id":"cow",
"color":"#0000FF",
"sound":"cow.wav",
"vibrate_timings":[
"0.0s",
"0.2s",
"0.1s",
"0.2s",
"0.1s",
"1.5s"
],
"visibility":2,
"tag":"new_messages",
"click_action":"",
"body_loc_key":"",
"body_loc_args":[
],
"title_loc_key":"",
"title_loc_args":[
],
"image":"spoonsbuzzimage",
"notification_priority":4
}
},
"apns":null,
"token":"Bot"
}
}
I expected my devices to get a notification with the image I added to the payload as well as vibrate in the pattern I set using vibrate_timings. The notification does come in just fine and everything else works but neither the image or the custom vibrations are included.
It's been a long time, but if someone has this problem they should add the default_vibrate_timings field, like this:
{...
default_vibrate_timings: false,
vibrate_timings: [
"0.0s",
"0.2s",
"0.1s",
"o.2s"
],
...}
For reference: Documenation Link
As android google blog clearly mentioned that Android Nougat is not supporting setNumber and setContentInfo at all and even I have tested it on Android Nougat.
Line from Google blog:
In addition, the subtext now supersedes the role of content info and
number
So when I use setNumber for devices prior than Nougat and setSubText for Nougat then Nougat works perfectly only setSubText method works. But when I run it on Devices running prior versions they run both methods as setNumber and setSubText.
So how I can handle this?
Why android prefer setSubText?
In addition what is the difference between setNumber and setContentInfo ?
So how I can handle this?
Your desired result is to have your notification display whatever you pass to setSubText() on Nougat and above and whatever you pass to setNumber() on Marshmallow and below. You can accomplish this by checking the Build info at runtime:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// your setSubText() code here
} else {
// your setNumber() code here
}
Why android prefer setSubText?
I can only really quote the blog post you linked here. They say:
"Many of the fields that were spread around the notifications have been collapsed into a new header row with your app’s icon and name anchoring the notification."
It sounds to me like a design-based preference.
In addition what is the difference between setNumber and setContentInfo ?
Documentation for setContentInfo(): "Set the large text at the right-hand side of the notification."
Documentation for setNumber(): "Set the large number at the right-hand side of the notification. This is equivalent to setContentInfo, although it might show the number in a different font size for readability." (emphasis added)
I implemented PushApps Android SDK.
All is well and working, but I want to create a notification by myself without using:
PushManager.buildNotification(intent.getExtras(), context,
NOTIFICATION_ID, R.drawable.notification_icon,
notificationIntent);
specific - I want to show a custom layout using the data received in the
intent.getExtras()
Thanks!
In order to show your own custom notification, you can take control over the PushAppsMessage interface. By doing that, you can show your own custom message with special layouts and more!. Checkout this article - How To Create Android Push Notifications With Custom View?.