我想知道是否有办法在项目中控制推送通知的声音和振动react-native
。
实际上我想让用户决定通知是否应该有声音/振动。用户应该能够完全禁用这些功能。
我到处都在搜索这个,但我的问题与典型问题有所不同:
- 我想让用户决定(是否应该禁用声音或振动),所以我想要一些包或具有
Javascript
实现的东西,以便我可以将此功能添加到我的应用程序的设置视图中。 - 我希望能够在 IOS 和 Android 中执行此操作。(如果您只了解这两种情况,请随时回答)
我想知道是否有办法在项目中控制推送通知的声音和振动react-native
。
实际上我想让用户决定通知是否应该有声音/振动。用户应该能够完全禁用这些功能。
我到处都在搜索这个,但我的问题与典型问题有所不同:
Javascript
实现的东西,以便我可以将此功能添加到我的应用程序的设置视图中。根据文档首先您需要创建频道
PushNotification.createChannel(
{
channelId: "channel-id", // (required)
channelName: "My channel", // (required)
channelDescription: "A channel to categorise your notifications", // (optional) default: undefined.
playSound: false, // (optional) default: true
soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
importance: 4, // (optional) default: 4. Int value of the Android notification importance
vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
},
(created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
);
并在本地通知对象中使用此频道 ID“频道 ID”
PushNotification.localNotification({
channelId: "channel_id",
vibrate: true,
title: "My Notification Title",
message: "My Notification Message",
onlyAlertOnce: false,
ignoreInForeground: false,
priority: "high",
});
你应该使用react-native-push-notification包。只需遵循文档即可。
安装包并将其链接到项目后。设置通知完成(来自文档):
PushNotification.localNotification({
/* Android Only Properties */
id: '0', // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
ticker: "My Notification Ticker", // (optional)
autoCancel: true, // (optional) default: true
largeIcon: "ic_launcher", // (optional) default: "ic_launcher"
smallIcon: "ic_notification", // (optional) default: "ic_notification" with fallback for "ic_launcher"
bigText: "My big text that will be shown when notification is expanded", // (optional) default: "message" prop
subText: "This is a subText", // (optional) default: none
color: "red", // (optional) default: system default
vibrate: true, // (optional) default: true
vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
tag: 'some_tag', // (optional) add tag to message
group: "group", // (optional) add group to message
ongoing: false, // (optional) set whether this is an "ongoing" notification
priority: "high", // (optional) set notification priority, default: high
visibility: "private", // (optional) set notification visibility, default: private
importance: "high", // (optional) set notification importance, default: high
/* iOS only properties */
alertAction: // (optional) default: view
category: // (optional) default: null
userInfo: // (optional) default: null (object containing additional notification data)
/* iOS and Android properties */
title: "My Notification Title", // (optional)
message: "My Notification Message", // (required)
playSound: false, // (optional) default: true
soundName: 'default', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
number: '10', // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
repeatType: 'day', // (optional) Repeating interval. Check 'Repeating Notifications' section for more info.
actions: '["Yes", "No"]', // (Android only) See the doc for notification actions to know more
});
该软件包适用于 Android 和 iOS ......两个平台的一个代码
需要提几点: