友盟推送支持自定义通知声音,在Android8.0以下,就很简单:
在您项目的res/raw/下放置命名为umeng_push_notification_default_sound。若无此文件,则默认使用系统的Notification声音
在Android8.0以上机型,那就需要自定义通知了,因为android8.0以上的Notification引入了Channel的概念,声音是定义在Channel里面的,所以我们需要重写的 UmengMessageHandler 的 getNotification方法,给大家写了个例子参考:
public static final String SOUND_PATH = "android.resource://com.test.demo/raw/umeng_push_notification_default_sound";
UmengMessageHandler messageHandler = new UmengMessageHandler() {
@Override
public Notification getNotification(Context context, UMessage msg) {
Uri soundUri = Uri.parse(SOUND_PATH);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("default", "Default_Channel", NotificationManager.IMPORTANCE_HIGH);
channel.setSound(soundUri, Notification.AUDIO_ATTRIBUTES_DEFAULT);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "default");
RemoteViews myNotificationView = new RemoteViews(context.getPackageName(),
R.layout.notification_view);
myNotificationView.setTextViewText(R.id.notification_title, msg.title);
myNotificationView.setTextViewText(R.id.notification_text, msg.text);
myNotificationView.setImageViewBitmap(R.id.notification_large_icon,
getLargeIcon(context, msg));
myNotificationView.setImageViewResource(R.id.notification_small_icon,
getSmallIconId(context, msg));
builder.setContent(myNotificationView)
.setSmallIcon(getSmallIconId(context, msg))
.setSound(soundUri)
.setTicker(msg.ticker)
.setAutoCancel(true);
return builder.build();
}
};
其中 notification_view 的自己写的通知的样式布局,给大家贴一下代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="64dp">
<RelativeLayout
android:id="@+id/upush_notification1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp">
<ImageView
android:id="@+id/notification_large_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginLeft="10dp"
android:src="@drawable/ic_launcher"
android:scaleType="fitXY" />
<TextView
android:id="@+id/notification_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:layout_toRightOf="@+id/notification_large_icon"
android:maxLines="1"
android:text="Title"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android: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_marginTop="3dp"
android:layout_toRightOf="@+id/notification_large_icon"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="false"
android:fadingEdge="horizontal"
android:singleLine="true"
android:text="Message"
android:textColor="#000000" />
<requestFocus />
</RelativeLayout>
<RelativeLayout
android:id="@+id/upush_notification2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<ImageView
android:id="@+id/notification_small_icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
</RelativeLayout>
</RelativeLayout>
写完自定义的Notification之后,通过友盟推送的 PushAgent set 一下就可以了:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mPushAgent.setMessageHandler(messageHandler);
}
打完收工,在友盟后台推送一条试一下就可以了