Android 极光推送 自定义通知铃声

公司使用的推送功能使用的是极光推送,对于普通的通知,极光推送API自动集成了声音的提示,但是对于一些特殊的情况,我们就需要自定义声音提示。
有点坑的是极光并没有提供设置自定通知铃声的接口,所以只能自己来写一个通知铃声。相对应的通知栏的显示也要自定义。

这里我们如果使用自定义铃声就需要在发送推送的时候,使用附加字段消息,在里面自定定义一个附加字段,然后在接收推送的时候根据那个附加字段消息来判断使用对应类型的铃声

导入极光推送就不多说了,根据官方API导入就行了
在AndroidMainfest.xml定义一个自定义的广播类

        <receiver
        //自定义的广播类用于接收推送
            android:name="cn.cerc.summer.android.core.MyBroadcastReceiver"
            android:enabled="true">
            <intent-filter>

                <!-- Required 用户注册SDK的intent -->
                <action android:name="cn.jpush.android.intent.REGISTRATION" />
                <!-- Required 用户接收SDK消息的intent -->
                <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
                <!-- Required 用户接收SDK通知栏信息的intent -->
                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
                <!-- Required 用户打开自定义通知栏的intent -->
                <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
                <!-- 接收网络变化 连接/断开 since 1.6.3 -->
                <action android:name="cn.jpush.android.intent.CONNECTION" />

                <category android:name="应用的包名" />
            </intent-filter>
        </receiver>

其他一些内容的导入请参考官方API

下面是MyBroadcastReceiver.java 自定义广播类

public class MyBroadcastReceiver extends BroadcastReceiver {

private static final String ACTION_NOTIFICATION_OPENED = JPushInterface.ACTION_NOTIFICATION_OPENED;
private final String EXTRA = "cn.jpush.android.EXTRA";
private final String NOTIFI_ID = "cn.jpush.android.NOTIFICATION_ID";
private final String MSG_ID = "MSG_ID";
private static String TAG = "print";
@Override
public void onReceive(Context context, Intent intent) {
    try {
        Bundle bundle = intent.getExtras();
        Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.e(TAG, "[MyReceiver] 接收Registration Id : " + regId);

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            //processCustomMessage(context, bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, " 获取推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            //bundle.get(JPushInterface.EXTRA_ALERT);推送内容
            Log.d(TAG, "获取推送下来的通知的ID: " + notifactionId);

            //获取到的推送的通知类型
            String str_Test = bundle.getString(JPushInterface.EXTRA_ALERT);

            //在这里自定义通知声音
            processCustomMessage(context,bundle);
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "打开了通知");
            Intent i = new Intent(context, MainActivity.class);
            i.putExtra("data","msgId");
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..

        } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
            Log.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
        } else {
            Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
        }
    } catch (Exception e) {

    }

}

/**
 * 自定义推送的声音
 *
 * @param context
 * @param bundle
 */
private boolean processCustomMessage(Context context, Bundle bundle) throws JSONException {
    JSONObject json = null;
    NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
    //这一步必须要有而且setSmallIcon也必须要,没有就会设置自定义声音不成功
    notification.setAutoCancel(true).setSmallIcon(R.mipmap.deting);
    /**
    *  这个地方特殊说明一下
    *  因为我们发出推送大部分是分为两种,一种是在极光官网直接发送推送,第二是根据后台定义
    * 的NOTIFICATION发送通知,所以在这里你要根据不同的方式获取不同的内容
    **/
    //如果是使用后台定的NOTIFICATION发送推送,那么使用下面的方式接收消息
    String alert = bundle.getString(JPushInterface.EXTRA_EXTRA);  //扩展消息
    String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);  //推送标题
    String msg = bundle.getString(JPushInterface.EXTRA_ALERT);   //推送消息

     //如果是直接使用极光官网发送推送,那么使用这个来接收消息内容
     // String title = bundle.getString(JPushInterface.EXTRA_TITLE);
     //String msg = bundle.getString(JPushInterface.EXTRA_MESSAGE);
     //  String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);

    //alert属于附加字段消息 这里我们增加了一个sound的附加字段,判断sound="trade_mall”时则播放自定义的消息,即:R.raw.test就是自己定义的声音,这个是放在raw文件夹内的,需要自己提供
    如果没有sound字段,或者字段内容不是trade_mall,这里就直接返回false ,再继续使用系统的声音。这样就可以实现再特殊情况使用自定义铃声,也可以在一般情况使用系统铃声
    json = new JSONObject(alert);
    if (json.has("sound")) {
        if (json.getString("sound").equals("trade_mall")) {
            notification.setSound(
                    Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.test));
        } else {
            return false;
        }
    } else {
        return false;
    }
    //这是点击通知栏做的对应跳转操作,可以根据自己需求定义
    Intent mIntent = new Intent(context, MainActivity.class);
    mIntent.putExtra("msgId", json.getString("msgId"));
    mIntent.putExtras(bundle);
    mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0);

    notification.setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .setContentText(msg)
            .setContentTitle(title.equals("") ? "title" : title)
            .setSmallIcon(R.mipmap.deting);
    //获取推送id
    int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
    //bundle.get(JPushInterface.EXTRA_ALERT);推送内容
    //最后刷新notification是必须的
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
    //

这里需要特别注意一下的是

,我们使用新的自定义notification,
一般情况接收推送时通知栏会有两个相同的推送显示出来这肯定不是我们想要的,
再上几行代码里面获取到了推送id,我们再notification刷新的时候将此id放进去,就会覆盖上一个通知
notificationManager.notify(notifactionId, notification.build()); //在此处放入推送id —notifactionId
return true;
}

以上便是实现自定义推送铃声的方法,参考案例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值