友盟推送自定义通知声音

友盟推送支持自定义通知声音,在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);
   }

打完收工,在友盟后台推送一条试一下就可以了

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值