【发布时间】:2020-04-21 15:16:44
【问题描述】:
我是原生反应新手,正在尝试为 android 创建推送通知。
我正在使用 PubNub 的以下教程。
当我完成教程后在 android studio 模拟器中运行我的应用程序时,我收到以下错误。
不太清楚如何解决它意味着什么,因为当我用谷歌搜索问题时没有出现。
这是我的代码
import React from 'react';
import PushNotificationIOS from 'react-native';
import PubNubReact from 'pubnub-react';
const PushNotification = require('react-native-push-notification');
export default class App extends React.Component {
constructor(props) {
super(props);
this.pubnub = new PubNubReact({
publishKey: 'YOUR_PUBNUB_PUBLISH_KEY_HERE',
subscribeKey: 'YOUR_PUBNUB_SUBSCRIBE_KEY_HERE'
});
this.pubnub.init(this);
PushNotification.configure({
// Called when Token is generated.
onRegister: function(token) {
console.log( 'TOKEN:', token );
if (token.os == "ios") {
this.pubnub.push.addChannels(
{
channels: ['notifications'],
device: token.token,
pushGateway: 'apns'
});
// Send iOS Notification from debug console: {"pn_apns":{"aps":{"alert":"Hello World."}}}
} else if (token.os == "android"){
this.pubnub.push.addChannels(
{
channels: ['notifications'],
device: token.token,
pushGateway: 'gcm' // apns, gcm, mpns
});
// Send Android Notification from debug console: {"pn_gcm":{"data":{"message":"Hello World."}}}
}
}.bind(this),
// Something not working?
// See: https://support.pubnub.com/support/solutions/articles/14000043605-how-can-i-troubleshoot-my-push-notification-issues-
// Called when a remote or local notification is opened or received.
onNotification: function(notification) {
console.log( 'NOTIFICATION:', notification );
// Do something with the notification.
// Required on iOS only (see fetchCompletionHandler docs: https://reactnative.dev/docs/pushnotificationios)
// notification.finish(PushNotificationIOS.FetchResult.NoData);
},
// ANDROID: GCM or FCM Sender ID
senderID: "sender-id",
});
}
}
【问题讨论】:
-
这篇文章能回答你的问题吗? stackoverflow.com/questions/61080350/…
-
我已经准备好看到这篇文章并按照它说的做了,但遗憾的是它没有用
-
感谢您的确认。但是你的问题和那个不同吗?我只需要知道我是否应该将 PubNub 工程师的注意力集中在一个或两个上。
-
你使用的是什么版本的
pubnub-react? -
@AreWojciechowski - 根据后面的教程,它应该是 pubnub-react@2.0.0
标签: react-native react-native-android pubnub