且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当应用程序处于活动状态时通过FCM发送通知

更新时间:2023-01-09 22:49:21

我在应用程序中使用了ionic和cordova.

I use ionic and cordova in my app.

我正在使用插件[cordova-plugin-firebasex]( https://github. com/dpa99c/cordova-plugin-firebasex )以接收推送.

I am using the plugin [cordova-plugin-firebasex] (https://github.com/dpa99c/cordova-plugin-firebasex) to receive push.

要发送推送,请使用以下json:

To send the push, use the following json:

{
  "registration_ids": [
      "token"
  ],
  "notification":{
    "title":"Ionic 4 Notification",
    "body":"Notification sent from POSTMAN",
    "sound":"default",
    "click_action":"FCM_PLUGIN_ACTIVITY",
    "icon":"notification_icon"
  },
  "data":{
    "email":"teste@gmail.com"
  },
  "priority":"high"
}

对于android,通知"该字段在应用程序处于后台时显示通知.

For android, the "notification" field that displays a notification when the application is in the background.

如果您处于前台,则必须使用插件[cordova-plugin-local-notifications]( https://github.com/katzer/cordova-plugin-local-notifications )

If you are in foreground, you have to display a notification yourself using the plugin [cordova-plugin-local-notifications] (https://github.com/katzer/cordova-plugin-local-notifications)

我的代码:

constructor(private firebaseCordova: FirebaseX) {}

private initializePushApp() {
    this.checkNotificationPermission(false);
    this.onMessageApp();
}

checkNotificationPermission(requested) {
    try {
        this.firebaseCordova.hasPermission().then(hasPermission => {
            if (hasPermission) {
                this.getTokenApp();
            } else if (!requested) {
                this.firebaseCordova.grantPermission().then(value => {
                    this.checkNotificationPermission(true);
                });
            } else {
                // Denied
                console.log("Notifications won't be shown as permission is denied");
            }
        });
    } catch (e) {
        console.error(e);
    }
}

onMessageApp() {
    try {
        this.firebaseCordova.onMessageReceived().subscribe(data => {
            this.showNotificationCordova(data);
        });
    } catch (e) {
        console.error(e);
    }
}

showNotificationCordova(notification) {
    if ('background' === notification.tap) {
        // click on notification in background
        alert(notification.title);
    } else {
        this.clickNotificationSub = this.localNotifications.on('click').subscribe(data => {
            // click on notification in foreground
            alert(notification.title);
        });

        this.localNotifications.hasPermission().then(permission => {
            if (permission) {
                this.localNotifications.schedule({
                    id: 1,
                    title: notification.title,
                    text: notification.body,
                    icon: notification.image ? notification.image : 'notification_icon',
                });
            } else {
                this.localNotifications.requestPermission().then(value => {
                    if (value) {
                        this.localNotifications.schedule({
                            id: 1,
                            title: notification.title,
                            text: notification.body,
                           icon: notification.image ? notification.image : 'notification_icon',
                        });
                    } else {
                        console.log("Notifications won't be shown as permission is denied");
                    }
                });
            }
        });
    }
}

当您输入此条件'background'=== notification.tap`时,将在后台单击该通知

When you enter this condition 'background' === notification.tap` the notification was clicked in the background