gpt4 book ai didi

javascript - 当应用程序处于后台时发送鸟推送通知

转载 作者:行者123 更新时间:2023-12-04 23:58:02 24 4
gpt4 key购买 nike

这是我的查询。我已经在我的一个 react-native 应用程序中实现了 sendbird sdk 来实现聊天。我正在尝试实现推送通知。如 sendbird 的文档中所述,我已将 react-native-firebase 用于 firebase 推送通知。现在的问题是我的 android 应用程序在前台时收到推送通知。为此触发 OnMessageReceived() 监听器。 但是当我的应用程序处于后台时,我没有收到来自 sendbird 的任何推送通知。没有触发任何 firebase 通知监听器。

此外,当我尝试从 firebase 控制台发送通知时,我收到了前台和后台通知。

希望得到你们的回应,因为这可以帮助我成功实现这个功能。

我的通知监听器代码与此类似。但是,这里并没有添加显示通知代码。

async componentDidMount() {
this.checkPermission();
this.createNotificationListeners(); //Notification listener
}

async createNotificationListeners() {
/*
* Triggered when a particular notification has been received in foreground
* */
this.notificationListener = firebase.notifications().onNotification((notification) => {
const { title, body } = notification;
this.showAlert(title, body);
});

/*
* If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows:
* */
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
const { title, body } = notificationOpen.notification;
this.showAlert(title, body);
});

/*
* If your app is closed, you can check if it was opened by a notification being clicked / tapped / opened as follows:
* */
const notificationOpen = await firebase.notifications().getInitialNotification();
if (notificationOpen) {
const { title, body } = notificationOpen.notification;
this.showAlert(title, body);
}
/*
* Triggered for data only payload in foreground
* */
this.messageListener = firebase.messaging().onMessage((message) => {
//process data message
console.log(JSON.stringify(message));
});
}

showAlert(title, body) {
Alert.alert(
title, body,
[
{ text: 'OK', onPress: () => console.log('OK Pressed') },
],
{ cancelable: false },
);
}
}

我的 AndroidManifest.xml 代码如下:-

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application ....

<activity android:name="com.facebook.devsupport.DevSettingsActivity"/>
<service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService" android:exported="false">

<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService"/>

<meta-data androd:name="com.google.firebase.messaging.default_notification_icon"
androd:resource="@mipmap/ic_launcher_logo"/>

<meta-data androd:name="com.google.firebase.messaging.default_notification_channel_id"
androd:value="test-channel"/>

</application>

最佳答案

所以最后经过大量研究,我引用了Sendbird的示例应用程序,成功地得到了解决方案,粘贴下面的链接。

https://github.com/sendbird/SendBird-JavaScript/tree/master/react-native-redux-sample

解决方案:-

您需要在App中注册 channel 。 Js componentDidMount()。

//应用js代码

componentDidMount(){
const channel = new firebase.notification.Android.Channel(
"YOUR-CHANNEL-ID",
"CHANNEL NAME",
firebase.notification.Android.Importance.Max).setDescription("description);
firebase.notifications().android.createChannel(chanel);


//don't forget to add handler for app state change
Appstate.addEventListener('change',this._handleAppStateChange);

)
}

_handleAppStateChange = currentAppState =>{
const sb = SendBird.getInstance();
if(sb)
{
if(currentAppState == "active")
{
sb.setForegroundState();
}
else if(currentAppState == "background")
{
sb.setBackgroundState();
}
}

//现在在主 index.js 中添加以下行

import backgroundPush from './Services/push';

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage',()=>backgroundPush) //add this line after appRegistry Component and this is a key to be added to get notitification in background when using sendbird, whicch is nowhere mentioned in its official doc. It is in the sample App I have also added link to it.

导入的 push/backgroundPush 是一个包含以下源的文件:-

push.js//虽然这个文件在示例应用程序中

import firebase from 'react-native-firebase'

export default async message => {
try {
const text = message.data.message;
const payload = JSON.parse(message.data.sendbird);
const localNotification = new firebase.notifications.Notification({
show_in_foreground: true
}).android
.setChannelId('channel_id')
.android.setSmallIcon('sendbird_ic_notification')
.android.setPriority(firebase.notifications.Android.Priority.High)
.setNotificationId(message.messageId)
.setTitle('New message')
.setSubtitle(`Unread message: ${payload.unread_message_count}`)
.setBody(text)
.setData(payload);
return firebase.notifications().displayNotification(localNotification);
} catch (e) {
return Promise.resolve();
}
};

对于我未收到的 Android 后台推送通知查询,这是对我有用的解决方案。虽然,仍在努力接收 iOS 端的推送通知。

谢谢。

关于javascript - 当应用程序处于后台时发送鸟推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61842116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com