I just stumbled upon the new MessagingStyle described here: https://developer.android.com/guide/topics/ui/notifiers/notifications.html (last paragraph)
I investigated further and found such a class also as a NotificationCompat.Style variant.
This is the code I tried:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
.notify(123, new NotificationCompat.Builder(this)
.setContentTitle("Test")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("4 new messages")
.setStyle(new NotificationCompat.MessagingStyle("Me")
.setConversationTitle("Team lunch")
.addMessage("Hi", 123, null) // Pass in null for user.
.addMessage("What's up?", 234, "Coworker")
.addMessage("Not much", 345, null)
.addMessage("How about lunch?", 456, "Coworker")).build());
}
}
This is basically just the default Activity generated when creating a new project and the sample code taken from the linked website.
Now my problem: The extended style is not shown on APIs below 24. I tested on a device with API 23. When running on an emulator with API 24, it works.
The documentation states:
Helper class for generating large-format notifications that include multiple back-and-forth messages of varying types between any number of people.
If the platform does not provide large-format notifications, this method has no effect. The user will always see the normal notification view. [...]
But versions since KitKat do provide large-format notifications.
Is the documentation not clear enough or am I doing something wrong?
Update: this is fixed as of the 25.0.0 Support Library release and NotificationCompat.MessagingStyle now backports much of the styling to previous versions of Android.
Previous answer:
As per this bug report, MessagingStyle currently does not do any special formatting prior to Android N. The bug report is marked as FutureRelease which means that the work is done and it will support pre-N devices with a more rich formatting in a future version of the Support Library.
If you wish to use it now, you can certainly build your own pre-N version of the notification (using a BigTextStyle if the version of Android is less than N for example).
If I am reading the source code correctly, the fallback for pre-24 devices has not been implemented as of the time of this writing. So, at the moment, it gives you something that compiles, and delegates properly to the native implementation on API Level 24+ devices, but will not show the messages on older devices.
Additional note: check your imports and make sure you use the android.support.v7.app.NotificationCompat class
instead of android.support.v4.app.NotificationCompat, because even in 25.2.0, using MessagingStyle with the "v4" builder does not show anything in devices with API < 24.
Related
I'm trying to get rid of the warnings that EXTRA_SHORTCUT... is deprecated from API 26 forward. My code seems to work but the icon isn't happening on the home page.
private void installShortcut(Context c) {
Intent intent = new Intent(
c.getApplicationContext(), c.getClass());
intent.setAction(Intent.ACTION_MAIN);
if (Build.VERSION.SDK_INT >= 26) {
ShortcutInfo shortcutInfo
= new ShortcutInfo.Builder(c, "shortcutID")
.setShortLabel(appName)
.setIcon(
createWithResource(
c, R.drawable.qmark))
.setIntent(intent)
.build();
ShortcutManager sm
= c.getSystemService(ShortcutManager.class);
sm.createShortcutResultIntent(shortcutInfo);
}
else ...
I can step through the code with the debugger and everything seems to work. I'm not seeing any warnings on log cat. And I can't find any current answers to this question.
Thanks
Steve S.
I found a similar question that led me to the answer. It turned out you now need to use a pinned sshortcut. I found an exclent demo on AndroidAthority by Jessica Thornsby dated March 20, 2018 about implementing shortcuts on Nougat and Oreo that put it all together for me.
Please see my reply to a very similar question for my code.
I put a simple app that does nothing but install a shortcut for itself on your homepage on github. It works for versions before and after Android 8. Pre Android 8 uses the sendBroadcast method and after creats a pinned shortcut.
In Android 6.0/API23 and earlier, the following used to work:
String settingEnabled = android.provider.Settings.Secure.getString(this.getContentResolver(), "enabled_notification_listeners");
In Android 7.0 Nougat/API24 this seems to be no longer supported, because the code above returns null.
It actually was never mentioned here: https://developer.android.com/reference/android/provider/Settings.Secure.html
How do we check if our app has notification access in Android 7.0 Nougat API24?
Edit: It seems that actually that after you first gained the access in the settings, the code above returns the correct state. But not on the initial request after installation.
Use this:
Set<String> packageNames = NotificationManagerCompat.getEnabledListenerPackages (context);
Ever since the new Google Cast SDK v3 has been out, I've been thinking about migrating to it, as it has a few features I need and I'm always interested in keeping everything as up-to-date as possible. My previous setup used Google Cast SDK v2 + Cast Companion Library. However I've been unable to find a way to provide a completely custom notification in the new Google Cast SDK v3 which prevents me from using it.
With Google Cast SDK v2 + Cast Companion Library I was able to simply subclass the VideoCastNotificationService and override its build method to make a custom notification:
public class MyCastNotificationService extends VideoCastNotificationService {
#Override
protected void build(MediaInfo info, Bitmap bitmap, boolean isPlaying) {
// ... build the custom notification
mNotification = new NotificationCompat.Builder(this)/*...*/.build();
}
}
Then I could register the MyCastNotificationService in my CastConfiguration like this:
CastConfiguration castConfig = new CastConfiguration.Builder(APPLICATION_ID)
/*...*/
.setCustomNotificationService(MyCastNotificationService.class)
.build();
However, with Google Cast SDK v3 I've been unable to find a replacement that works. Here are the results of the bit of research I've done on this topic:
The MediaNotificationService seems to be the component replacing the VideoCastNotificationService, however it does not seem to have a build method or anything similiar.
Both the NotificationOptions.Builder and CastMediaOptions.Builder classes don't seem to have any methods for registering a custom notification service.
The only documented way of customizing the notification seems to be custom action buttons as shown in the Add Media Controls to Notification and Lock Screen section, however I need to create a notification using the RemoteViews API, so this is not sufficient for me.
Has anyone been able to provide a custom notification using the new Google Cast SDK v3? As far as I can tell, this seems unsupported, but I'd love to be proven wrong here. Thanks in advance!
I'm just testing my app on Android 5.0 and I discover that I am no more able to get the RemoteViews from third party notifications to read its title and ticker text like I did in KitKat. The code I used successfully on KitKat is similar to this:
public static List<String> getText(Notification notification) {
RemoteViews views = notification.contentView;
if (views == null)
return null;
else {
...
}
}
This function return me NULL so it isn't able to grab the entire contentView from the notification. Any help?
Thanks in advance!
I've just figured out that the correct approach is to use AccesssibilityService for Android <= JELLY_BEAN_MR2 and to extend the NotificationListenerService for other newer versions.
In the new class, extension of NotificationListenerService, it is necessary to add the declaration #TargetApi(Build.VERSION_CODES.THE_OS_NAME_YOU_WANT) on the top, to ensure that the following code will be executed just starting by that version of the OS.
It works like it should.
I hope it will help someone else.
Thank you anyway
So this is more or less a continuation of my last question Link to last post
I have been working on the code and a buddy of mine suggested I use Notifiction.Builder so I have that figured out. I am wondering how I or anyone else would go about making status notifications "Cross API" What I mean by this is that Builder works on API 11+ but anything below is a no go. Are there any known work around's / fixes for this issue or Will I need to code two different versions of this?
There are a number of different ways to achieve multiple api support using deprecated methods, the most frequently mentioned of which is reflection. Personally, I went with exploiting the classloader to solve the notification problem. I made a base abstract class with basic wrapper methods for handling the different data required for creating a notification, which looks like this:
public abstract class NotificationWrapper {
public NotificationWrapper(Context context) {
}
public abstract void createNotification(int resid, CharSequence ticker);
public abstract void setTitle(CharSequence title);
public abstract void setText(CharSequence text);
public abstract void setIntent(PendingIntent intent);
public abstract void setFlags(int... flags);
public abstract void setDefaults(int defaults);
public abstract Notification getNotification();
}
You can of course add and remove methods depending on how much you're customizing your notifications. Then you can just extend this to three separate classes: one for API <11, one for API >=11, and one for Jelly Bean and up. In each class you just relay these method calls to the appropriate available methods for the current API version (i.e. pre-11 relays to Notification methods, post-11 relays to Notification.Builder methods, and Jelly Bean is the same as post-11 except it uses build() as opposed to getNotification()). Then, when it comes time to create your notification you can use a conditional on the SDK version and the beauties of polymorphism to load the right subclass and build your notification using its wrapper methods:
NotificationWrapper wrapper = null;
int ver = VERSION.SDK_INT;
if (ver >= 16) {
wrapper = new NotificationWrapJB(context);
} else if (ver >= 11) {
wrapper = new NotificationWrapHC(context);
} else {
wrapper = new NotificationWrapPreHC(context);
}
wrapper.createNotification(resId, tickerText);
//etc....
Again, this is just one of many ways to deal with this problem, but I like it because it just feels more organized.
If you're unfamiliar with which methods to use for the different SDK versions, read the Android docs on Notification and Notification.Builder.
I think you're looking for NotificationCompat present in the Support Library.
And if you're looking for the brand New Notifications coming with Jelly Bean you can use NotificationCompat2 from Jake Wharton (the one that made ActionBarSherlock).