【发布时间】:2020-07-23 14:41:21
【问题描述】:
我需要从设备(而不是后端)发送通知有没有办法做到这一点?
感谢您的回复,我将不胜感激
【问题讨论】:
-
嗨,欢迎来到 StackOverflow!如果您有任何相关代码可以帮助其他人更好地理解您的问题,请将其包含在您的问题中。请参阅How to ask 了解更多信息。
标签: huawei-mobile-services huawei-developers
我需要从设备(而不是后端)发送通知有没有办法做到这一点?
感谢您的回复,我将不胜感激
【问题讨论】:
标签: huawei-mobile-services huawei-developers
正如其他人所提到的,您可以为此目的使用一些库。其中我相信“react-native-push-notification”得到了更广泛的认可:https://www.npmjs.com/package/react-native-push-notification
请注意,如果您使用的是 RN0.60.+,它将为您自动链接,您无需执行 react-native 链接或在 MainApplication.java 中添加包等步骤。如果没有,repo 本身也有详细的安装说明。
此外,该库的一部分依赖于 gms/firebase,因此在 HMS 上下文中您不能使用它的某些功能,例如远程通知。如果您的目的仅仅是本地通知,那么您就可以开始了。
另外请注意,如果您打算处理本地通知的点击,则需要配置 onNotificaiton 来监听点击。需要注意的一点是,在 HMS 设备上,requestPermission 参数必须设置为 false,否则会调用 firebase 依赖项并导致错误。
示例代码sn-ps:
import PushNotification from 'react-native-push-notification'
export default class App extends Component {
...
componentDidMount() {
...
PushNotification.configure({
// (required) Called when a remote is received or opened, or local notification is opened
onNotification: function (notification) {
console.log("NOTIFICATION:", notification);
// process the notification
// (required) Called when a remote is received or opened, or local notification is opened
notification.finish(...);
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,
/**
* (optional) default: true
* - Specified if permissions (ios) and token (android and ios) will requested or not,
* - if not, you must call PushNotificationsHandler.requestPermissions() later
* - if you are not using remote notification or do not have Firebase installed, use this:
* requestPermissions: Platform.OS === 'ios'
*/
requestPermissions: false,
});
...
}
...
onSendLocalNotification = () => {
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
allowWhileIdle: false, // (optional) set notification to work while on doze, default: false
ignoreInForeground: false, // (optional) if true, the notification will not be visible when the app is in the foreground (useful for parity with how iOS notifications appear)
/* iOS only properties */
alertAction: "view", // (optional) default: view
category: "", // (optional) default: empty string
userInfo: {}, // (optional) default: {} (using null throws a JSON value '<null>' error)
/* 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
});
}
...
}
希望你能使用 Code Pattern。
【讨论】: