极光React Native 实现iOS和Android自定义消息推送通知声音

1.铃声要在注册通知渠道时声明,发通知时声明无效,渠道只能注册一次

1.铃声要在注册通知渠道时声明,发通知时声明无效,渠道只能注册一次

public class MainActivity extends ReactActivity {

@Override

public void onCreate(Bundle savedInstanceState) { 

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

String channelId = "112";

String channelName = "消息";

int importance = NotificationManager.IMPORTANCE_HIGH;

createNotificationChannel(this, channelId, channelName, importance);

}

}

@TargetApi(Build.VERSION_CODES.O)

private void createNotificationChannel(Context reactContext, String channelId, String channelName, int importance) {

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

NotificationChannel mChannel = new NotificationChannel(channelId, channelName, importance);

// 自定义声音

mChannel.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification),null);

mNotificationManager.createNotificationChannel(mChannel);

}

2.下面修改极光jpush-react-native的源码

public static class JPushReceiverextends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent data) {

if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(data.getAction())) {

mCachedBundle = data.getExtras();

try {

String message = data.getStringExtra(JPushInterface.EXTRA_MESSAGE);

Logger.i(TAG,"收到自定义消息: " + message);

mEvent =RECEIVE_CUSTOM_MESSAGE;

if (mRAC !=null) {

sendEvent();

}

}catch (Exception e) {

e.printStackTrace();

}

}else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(data.getAction())) {

mCachedBundle = data.getExtras();

processCustomMessage(context,mCachedBundle);//极光源码里插入这句

try {

============下面方向非极光源码,自己实现的============

private void processCustomMessage(Context context, Bundle bundle) {

NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

NotificationCompat.Builder notification =new NotificationCompat.Builder(context,"companion");

String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);

int notification_id =0;

if (!TextUtils.isEmpty(extras)) {

JSONObject extraJson =null;

try {

String message =new JSONObject(extras).getString("message");

extraJson =new JSONObject(message);

if (extraJson.length() >0) {

Logger.i(TAG,"processCustomMessage" + extraJson.toString());

notification_id = extraJson.getInt("notification_id");

notification.setAutoCancel(true)

.setContentText(extraJson.getString("tips"))

.setSmallIcon(R.mipmap.ic_launcher)

.setContentTitle(extraJson.getString("title"));

}

}catch (JSONException e) {

e.printStackTrace();

}

}

Intent intent =new Intent();

intent.setClass(context, JPushReceiver.class);

intent.setAction(JPushInterface.ACTION_NOTIFICATION_OPENED);

intent.putExtras(bundle);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notification_id, intent, PendingIntent.FLAG_ONE_SHOT);

notification.setContentIntent(pendingIntent);

manager.notify(notification_id, notification.build());

}

你可能感兴趣的:(极光React Native 实现iOS和Android自定义消息推送通知声音)