跳转到主要内容
PRODUCT DOCUMENTS

快速找到所需文档,高效完成接入与排障

浏览产品文档与友盟 Skill,展开目录并阅读正文。

自定义通知栏样式代码示例

可以重写UmengMessageHandler类的getNotification方法来自定义通知样式,以下代码是一个示例:

//自定义通知栏样式@Overridepublic @Nullable Notification getNotification(Context context, UMessage msg) {
    Notification.Builder builder;
    if (Build.VERSION.SDK_INT >= 26) {
        NotificationChannel channel;
        channel = getDefaultModeChannel(context);
        builder = new Notification.Builder(context, channel.getId());
    } else {
        builder = new Notification.Builder(context);
    }
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.auto_notification_view);
    remoteViews.setTextViewText(R.id.notification_title, msg.title);
    remoteViews.setTextViewText(R.id.notification_text, msg.text);
    remoteViews.setImageViewBitmap(R.id.notification_large_icon, getLargeIcon(context, msg));
    remoteViews.setImageViewResource(R.id.notification_small_icon, getSmallIconId(context, msg));

    builder.setContent(remoteViews)
            .setSmallIcon(getSmallIconId(context, msg))
            .setTicker(msg.ticker)
            .setAutoCancel(true);

    return builder.getNotification();
}

private static NotificationChannel getDefaultModeChannel(Context context) {
    if (Build.VERSION.SDK_INT >= 26) {
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = manager.getNotificationChannel("custom");
        if (channel != null) {
            return channel;
        }
        channel = new NotificationChannel("custom",
                PushAgent.getInstance(context).getNotificationChannelName(),
                NotificationManager.IMPORTANCE_DEFAULT);
        //通过这个修改声音//          Uri uri = Uri.parse("android.resource://" +context.getPackageName() + "/" + R.raw.umeng_push_notification_default_sound );//          Settings.System.DEFAULT_NOTIFICATION_URI//          channel.setSound(uri, Notification.AUDIO_ATTRIBUTES_DEFAULT);

        manager.createNotificationChannel(channel);
        return channel;
    }
    return null;
 }

下面的xml代码表示的是自定义通知的样式,左边是一个大图标,右边是一个小图标,中间是两行文本, 分别显示了通知的标题、内容和自定义信息。

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="64dp" ><ImageViewandroid:id="@+id/notification_large_icon"android:layout_width="64dp"android:layout_height="64dp"android:scaleType="fitXY"android:src="@drawable/ic_launcher" /><TextViewandroid:id="@+id/notification_title"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="5dp"android:layout_toRightOf="@+id/notification_large_icon"android:text="Title" /><TextViewandroid:id="@+id/notification_text"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_below="@+id/notification_title"android:layout_marginLeft="10dp"android:layout_toRightOf="@+id/notification_large_icon"android:text="Message" /><ImageViewandroid:id="@+id/notification_small_icon"android:layout_width="24dp"android:layout_height="24dp"android:scaleType="fitXY"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:layout_marginRight="5dp"android:layout_marginBottom="5dp"android:src="@drawable/ic_launcher" /></RelativeLayout>

若需使用自定义的通知样式,需要在创建通知时,判断消息中对应的通知样式编号(msg.builder_id),App基于编号开发(预置)需要展现的样式。