【问题标题】:Sending notifications via FCM when app is active当应用程序处于活动状态时通过 FCM 发送通知
【发布时间】:2020-09-10 15:43:45
【问题描述】:

即使应用程序在前台,我如何才能留下未读的推送消息?

我正在使用 ionic 创建一个应用程序。我使用 Firebase 云消息传递(cordova-plugin-fcm-with-dependecy-updated)的推送通知来订阅消息,并使用后端进行发送。 后端请求示例:

{
   "headers": {
      "Authorization": "key = blahblah",
      "Content-Type": "application / json",
      "project_id": "111111111111"
   },
   "json": {
      "registration_ids": [
         "token"
      ],
      "time_to_live": 1200,
      "data": {
         "title": "title",
         "body": "body",
         "sound": "default",
         "badge": 1,
         "click_action": "FCM_PLUGIN_ACTIVITY"
      }
   }
}

我还尝试发送通知键而不是日期键。尝试添加到root

{
    ...
    "android": {
        "ttl": "1200s",
        "priority": "high",
        "notification": {
            "click_action": "FCM_PLUGIN_ACTIVITY"
        }
    },
    ...
    "apns":{
        "headers":{
            "apns-priority":10,
            "apns-expiration": date('U') + 1200
        },
        "payload":{
            "aps":{
                "badge":1,
                "category":"FCM_PLUGIN_ACTIVITY"
            }
        }
    }
}

结果是一样的——当应用在后台时,推送是不显示在托盘中的。

如果应用程序在前台,我如何才能让推送未读,并仅在用户点击时在this.fcm.onNotification().subscribe(...) 调用我的操作?

附言我尝试使用 cordova-plugin-local-notification,但使用它会导致一些冲突 - subscribe 操作不会在 ios 中触发

【问题讨论】:

  • 我建议您使用 this Cordova plugin 通过 Firebase 推送通知,因为它经常更新,包括我自己在内的许多开发人员都在生产应用程序中使用它。我可以确认这个插件在应用程序处于前台、后台和关闭时都能正常工作。

标签: android ios ionic-framework firebase-cloud-messaging cordova-plugin-fcm


【解决方案1】:

我在我的应用中使用 ionic 和 cordova。

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

要发送推送,请使用以下 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,当应用程序在后台时显示通知的“通知”字段。

如果您在前台,则必须使用插件 [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`时,通知在后台被点击了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多