有关推送、自定义推送铃声

2023-10-11  本文已影响0人  雨泽Sunshine

一.有关Android推送

1.常见推送通道

FCM通道:Firebase Cloud Messaging,是一项针对Android、iOS及Web的消息与通知的跨平台解决方案,由Google拥有的Firebase公司提供。

极光通道:Aurora Mobile,全面兼容和支持Android、iOS、鸿蒙、快应用和winphone平台,提供极光通道、APNS、FCM、华为、小米、OPPO、VIVO、魅族、华硕等系统级消息下发通道。

华为通道

小米通道

OPPO通道

VIVO通道

魅族通道

荣耀通道

2.离线是否可收到推送

Android:Android transport layer(ATL),仅适用于运行Google Play的安卓设备。

iOS:适用于Apple设备的Apple推送服务器(APNs)。

Web:Web应用的网络推送协议。

3.通知渠道

从Android 8.0(API级别26)开始,必须为所有通知分配渠道,否则通知将不会显示。通过将通知归类到不同的渠道中,用户可以停用应用的特定通知渠道(而非停用您的所有通知),还可以控制每个渠道的视觉和听觉选项。

在Android 7.1(API级别25)及更低版本的设备上,每个应用其实只有一个渠道,用户仅可以按应用来管理通知。

注意:界面将渠道称作“类别”。

微信的通知类别

4.通知的重要程度

从Android 8.0(API级别26)开始,渠道还可以指定通知的重要程度等级importance,因此,发布到同一通知渠道的所有通知的行为都相同。

二.iOS自定义推送提示音

1.格式要求

音频数据格式必须是Linear PCMMA4 (IMA/ADPCM)µLawaLaw中的一种,且时长要求在30s以下,否则就是系统默认的铃声。

可以将音频数据打包到aiffwavcaf文件中。

2.客户端设置

方式一:将声音文件导入工程,操作步骤:选中工程Target --> Build Phases --> Copy Bundle Resources。

导入声音文件

方式二:直接拖入到某文件夹下:

导入声音文件

3.服务端设置

指定iOS平台下的sound参数,具体传入值是声音文件名+后缀,例如FCM自定义声音格式:

{
   "message":{
      "token":"ce3_Fi431Eonq-_qPrvDP0:APA91bHr8R7-AXYbzw9JmwSi6C0rpx7hXABbKMtT1CJ5GrDhwozAXuBLmUOV3tRo3Nxt4DV6R3wmEI2AfVXhnw8XafNNksFLC5TnYmHq0XWpYVxOpP0yyDT19sCkPBshZGGUGhhPEJeq",
      "notification":{
        "title":"FCM Message",
        "body":"This is an FCM notification message!"
      },
      "apns":{
          "payload":{
              "aps":{
                  "sound":"叮咚.caf"
              }
          }
      }
   }
}

三.Android自定义推送提示音

1.格式要求

支持的文件格式包括mp3wavmpeg等。

2.极光/OPPO/FCM通道通知实现

a.客户端设置

<1>将音频文件拖入到src/main/res/raw文件夹下:

导入声音文件

<2>创建channel并设置sound:

const String channelGroupId = 'custom sound group';

const AndroidNotificationChannel channelHappy = AndroidNotificationChannel(
  'custom sound happy',
  '自定义铃声happy',
  description: '自定义铃声通道happy',
  importance: Importance.high,
  sound: RawResourceAndroidNotificationSound('happy'),
  groupId: channelGroupId,
);

const AndroidNotificationChannel channelSpring = AndroidNotificationChannel(
  'custom sound spring',
  '自定义铃声spring',
  description: '自定义铃声通道spring',
  importance: Importance.high,
  sound: RawResourceAndroidNotificationSound('spring'),
  groupId: channelGroupId,
);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

调用下面的方法创建:

Future<void> _createNotificationChannelGroup() async {
  /// 创建分组
  const androidNotificationChannelGroup = AndroidNotificationChannelGroup(
    channelGroupId,
    '自定义铃声分组',
    description: '用于自定义铃声的分组',
  );
  await flutterLocalNotificationsPlugin
    .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()!
    .createNotificationChannelGroup(androidNotificationChannelGroup);

  /// 创建通知渠道并关联到分组
  await flutterLocalNotificationsPlugin
    .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()!
    .createNotificationChannel(channelHappy);

  await flutterLocalNotificationsPlugin
    .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()!
    .createNotificationChannel(channelSpring);
}
设备上的自定义铃声通知渠道
b.服务器设置

推送时需要在notification下配置soundchannel_id字段,channel_id字段需与客户端创建的channel id一致。

服务端字段设置

例如FCM平台的服务端设置:

{
   "message":{
      "token":"fH-B3A9-Rky5jr-OlXC-Cj:APA91bGPOLYrkttswUrN9-maUZDUpbywTWptREHRMbwqCYSnvkTMio485Drfm1JQtmdRWgZ42h4NPp0zPLXU6iH7BPzE6RNkgoOW9P8hJU8H7hRvTzTTrb8eHUaKTC_S5l0yOPWuggs_",
      "notification":{
        "title":"FCM Message",
        "body":"This is an FCM notification message!"
      },
      "android":{
          "notification":{
              /// 如果 sound 和 channel_id都有传,那么会以 channel_id 的铃声为准
              /// Android 8.0以上,需传入channel_id,否则自定义铃声失效
              "channel_id": "custom sound happy",
              "sound":"happy"
          }
      }
   }
}

四.iOS推送语音播报

1.新增通知扩展Notification Service Extension

新增通知扩展

2.修改通知扩展支持的最低版本:

image.png

后续待定。。。

上一篇 下一篇

猜你喜欢

热点阅读