第十六章:自定义push notification sound

前面一节已经讲过如何在ionic中集成jpush,这样我们的hybrid app在部署到ios或者android上面的时候,就可以接收通知了。如果不满足系统自带的声音,可以通过一些方式来播放自定义的通知铃声。需要严格按照下面的步骤来操作。

ios:

主要思路:在发送push notification的时候指定sound文件名,并且在xcode编译ios的时候把指定的sound文件放到resource文件夹中;ionic中不需要做任何的改动。

  • 编译工程:ionic build ios
  • 使用xcode打开工程
  • 把铃声文件放到resource文件夹中
  • 安装到Iphone上
  • 使用jpush console测试(http://www.jpush.cn)

android:

主要思路:在接收到jpush的notification的时候,取消下一步的操作;转而使用android本身的NotificationBuilder来处理notification(需要在android sudio中指定声音文件),可自定义声音、icon等信息

  • ionic build android
  • 使用android studio打开工程
  • 在res/raw文件夹下面添加sound文件
  • 重写myreceiver中的handlingNotificationReceive,使之看起来如下:
 1    private void handlingNotificationReceive(Context context, Intent intent) {
 2         Log.i(TAG, "----------------  handlingNotificationReceive");
 3 
 4         Intent launch = context.getPackageManager().getLaunchIntentForPackage(
 5                 context.getPackageName());
 6         launch.addCategory(Intent.CATEGORY_LAUNCHER);
 7         launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
 8 
 9         String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);
10         JPushPlugin.notificationTitle = title;
11 
12         String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT);
13         JPushPlugin.notificationAlert = alert;
14 
15         Map<String, Object> extras = getNotificationExtras(intent);
16         JPushPlugin.notificationExtras = extras;
17 
18         NotificationManager notificationManager = (NotificationManager) context
19                 .getSystemService(Context.NOTIFICATION_SERVICE);
20         Notification.Builder mBuilder = new Notification.Builder(
21                 context)
22                 .setSmallIcon(R.drawable.icon)
23                 .setContentTitle(title)
24                 .setContentText(alert);
25 
26         Intent notificationIntent = new Intent(context,
27                 MainActivity.class);
28         PendingIntent intent1 = PendingIntent.getActivity(context, 0,
29                 notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
30 
31         mBuilder.setContentIntent(intent1);
32         Uri uri=Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.csound);
33         mBuilder.setSound(uri);
34         Notification notification = mBuilder.build();
35         notification.flags |= Notification.FLAG_AUTO_CANCEL;
36         notification.defaults |= Notification.DEFAULT_VIBRATE;
37         notificationManager.notify(0, notification);
38 
39 //        JPushPlugin.transmitNotificationReceive(title, alert, extras);
40     }
View Code
  • 安装到android手机上
  • 使用jpush console进行测试(http://www.jpush.cn)

参考:

IOS: local notifications and remote notifications(known as push notifications)

IOS(solution): customize sounds in push notifications

IOS: list of system sounds

IOS: playing system sound without importing any sounds

online media convertor

android: push notifications

android: stack overflow, how to custom push notifications

android: how to custom push notifications sound base on pushwoosh

android(solution): custom push notifications based on jpush api

原文地址:https://www.cnblogs.com/allanli/p/iOS_android_custom_push_notification_sound.html