简体   繁体   中英

Can I use NotificationCompat.BigTextStyle without crashing on older devices?

I don't have any device older than 4.1 to test on. I'm trying to test out pushing my notifications with the code below. It's mostly all taken from the Notification documentation . Would my code crash or does the NotificationCompat class handle all of this for me gracefully?

Under the "Handling Compatability" section it reads:

Handling compatibility

Not all notification features are available for a particular version, even though the methods to set them are in the support library class NotificationCompat.Builder. For example, action buttons, which depend on expanded notifications, only appear on Android 4.1 and higher, because expanded notifications themselves are only available on Android 4.1 and higher.

To ensure the best compatibility, create notifications with NotificationCompat and its subclasses, particularly NotificationCompat.Builder. In addition, follow this process when you implement a notification:

...

So does this mean that if I use the NotificationCompat class it will handle all of the compatibility for me?

My code that I'm worried about (because it uses BigTextStyle):

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Title")
                .setContentText(String.format("%s", message));
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(mContext, ActivityMain.class);

        // The stack builder object will contain an artificial back stack for
        // the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(MainActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        // Add max priority
        mBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
        // Add bigTextStyle
        NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
        bigTextStyle.bigText(String.format("%s", message));
        mBuilder.setStyle(bigTextStyle);
        mBuilder.setAutoCancel(true);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(1, mBuilder.build());

You have nothing to worry about.

Helper class for generating large-format notifications that include a lot of text. If the platform does not provide large-format notifications, this method has no effect. The user will always see the normal notification view.

( NotificationCompat.BigTextStyle )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM