我已经在React native中创建了推送通知。如果我从以下站点进行测试,我可以得到通知:https://pushtry.com/
但是现在,如果我尝试使用自定义通知对象。推送通知不起作用:
{“ _ data”:{“ created”:“ 19年8月21日09:24 AM”,“停车场”:“ 0”,“消息”:“您的交易即将过期!\ n1。 这是独家DOB交易-SN \ n总价格:免费\ n兑换日期:21 19年8月\ n到期日:19年8月23日\ n2。这是独家DOB交易- SN \ n总价:免费\ n兑换日期:19年8月21日\ n到期日期:8月23日 '19 \ n3。这是独家DOB交易-SN \ n总价:免费\ n兑换 日期:19年8月20日\ n有效日期:8月23日 '19“,” title“:” Notification“},” _ from“:” 759251633792“,” _ messageId“:” 0:1566810334866452%98a4273ef9fd7ecd“,” _ sentTime“:1566810334859,” _ ttl“:3600}
我为此使用了以下代码:
async componentDidMount() {
if (Platform.OS === 'android') {
await Helpers.requestPermission()
.then((result) => {
//alert(JSON.stringify(result)) //result==success then permission granted
})
.catch((error) => {
alert(error) //result==permission not granted
});
}
else {
}
this.checkPermission();
this.createNotificationListeners();
const notificationOpen: NotificationOpen = await firebase.notifications().getInitialNotification();
if (notificationOpen) {
const action = notificationOpen.action;
const notification: Notification = notificationOpen.notification;
var seen = [];
alert(JSON.stringify(notification.data, function (key, val) {
if (val != null && typeof val == "object") {
if (seen.indexOf(val) >= 0) {
return;
}
seen.push(val);
}
return val;
}));
}
const channel = new firebase.notifications.Android.Channel('test-channel', 'Test Channel', firebase.notifications.Android.Importance.Max)
.setDescription('My apps test channel');// Create the channel
firebase.notifications().android.createChannel(channel);
this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) => {
// Process your notification as required
// ANDROID: Remote notifications do not contain the channel ID. You will have to specify this manually if you'd like to re-display the notification.
});
this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
// Process your notification as required
notification
.android.setChannelId('test-channel')
.android.setSmallIcon('ic_launcher');
firebase.notifications()
.displayNotification(notification);
});
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
// Get the action triggered by the notification being opened
const action = notificationOpen.action;
// Get information about the notification that was opened
const notification: Notification = notificationOpen.notification;
var seen = [];
alert(JSON.stringify(notification.data, function (key, val) {
if (val != null && typeof val == "object") {
if (seen.indexOf(val) >= 0) {
return;
}
seen.push(val);
}
return val;
}));
firebase.notifications().removeDeliveredNotification(notification.notificationId);
});
}
//1
async checkPermission() {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
this.getToken();
} else {
this.requestPermission();
}
}
//3
async getToken() {
let fcmToken = await AsyncStorage.getItem('fcmToken');
if (!fcmToken) {
fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
// user has a device token
console.log('Token ------ > ' + fcmToken);
await Helpers.saveInPref(constant.PREF_DEVICE_TOKEN, fcmToken);
}
}
}
//2
async requestPermission() {
try {
await firebase.messaging().requestPermission();
// User has authorised
this.getToken();
} catch (error) {
// User has rejected permissions
console.log('permission rejected');
}
}
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));
});
}
请检查。让我知道是否需要其他代码