简体   繁体   English

我可以将 OneSignal 令牌导入 FCM 吗?

[英]Can I import OneSignal tokens to FCM?

I have several thousand OneSignal web push notification tokens I want to import to FCM.我有几千个 OneSignal web 推送通知令牌要导入 FCM。 Is there a way to do this?有没有办法做到这一点?

I see this endpoint which requires the https://fcm.googleapis.com/fcm/send/...key... endpoint that OneSignal gives me, but I don't know what to put in for auth and p256dh .我看到这个端点需要 OneSignal 给我的https://fcm.googleapis.com/fcm/send/...key...端点,但我不知道要为authp256dh

https://developers.google.com/instance-id/reference/server#create_registration_tokens_for_apns_tokens https://developers.google.com/instance-id/reference/server#create_registration_tokens_for_apns_tokens

So yes this can be done. 所以可以的。 First you will need to contact OneSignal support and get the public and private VAPID keys for your app. 首先,您需要联系OneSignal支持人员,并获取应用程序的公共和私有VAPID密钥。 Each app in your dashboard will have a different set. 信息中心中的每个应用都有不同的设置。

Next you will need to make an API call to OneSignal in order to export the users in a CSV file. 接下来,您需要对OneSignal进行API调用,以便将用户导出为CSV文件。

You can find the API url in the docs and use curl or use your favorite language. 您可以在文档中找到API网址,并使用curl或使用自己喜欢的语言。 I used Node + Axios to make my calls. 我使用Node + Axios拨打电话。 The API call will supply you with a link to download the CSV. API调用将为您提供下载CSV的链接。

Here is the documentation https://documentation.onesignal.com/reference#csv-export 这是文档https://documentation.onesignal.com/reference#csv-export

You want to make sure you add the "extra_fields" parameter to your request with the "web_auth" and "web_p256" fields added. 您要确保在添加了“ web_auth”和“ web_p256”字段的请求中添加“ extra_fields”参数。 The CSV will provide you with the other piece of the puzzle which is the endpoint url in their identifier column. CSV将为您提供另一个难题,即标识符列中的端点URL。

Once you have all this information you can now send pushes using a library such as web-push for Node 掌握了所有这些信息之后,您现在可以使用诸如web-push之类的库来发送推送

https://github.com/web-push-libs/web-push https://github.com/web-push-libs/web-push

Hope that helps! 希望有帮助!

EDIT 编辑

As Cedric stated the actual push payload is a little bit more complicated because you need to comply with the OneSignal Service worker data handling. 正如Cedric所说,实际的推送有效负载要稍微复杂一点,因为您需要遵守OneSignal Service工作程序的数据处理。

You can see the formatting starting at line 313 here 您可以在此处从第313行看到格式

If you are using a library like web-push for Node to send your push payloads your payload would be formatted something like this for a standard push to a OneSignal service worker. 如果您正在使用诸如web-push之类的Node库来发送您的推送有效载荷,则您的有效载荷将被格式化为标准推送到OneSignal服务工作者的格式。

const uuidv1 = require('uuid/v1')
const webpush = require('web-push') 

let subscription = {
    endpoint: 'USER ENDPOINT URL',
    keys: {
        auth: 'USER AUTH KEY',
        p256dh: 'USER P256 KEY'
    }
}

let vapid = { private: 'VAPID PRIVATE KEY', public: 'VAPID PUBLIC KEY' }

// Format Message for OneSignal Service Worker
let notification = JSON.stringify({
    custom: {
        i: uuidv1(), //Generate UUID for the OneSignal Service worker to consume
        u: 'CLICK URL'
    },
    title: 'TOP TITLE',
    alert: 'MESSAGE BODY',
    icon: 'ICON IMAGE URL'
})

webpush.setVapidDetails('mailto: sendError@YourEmail.com', vapid.public, vapid.private)
webpush.sendNotification(subscription, notification)

It's much more complex than Dan's answer. 它比Dan的答案复杂得多。 If your users don't subscribe to your own service worker, it won't work. 如果您的用户未订阅您自己的服务工作者,那么它将无法正常工作。 OS will send its default notification when an 'unknown' error occurs, which it will send "You have new updates" as a notification to the user even though you passed different payload. 发生“未知”错误时,操作系统将发送其默认通知,即使您传递了不同的有效负载,它也会向用户发送“您有新更新”作为通知。 You also need to pass: "custom": { "i": uuidv1() } to your payload for it to work. 您还需要将: "custom": { "i": uuidv1() }传递给您的有效负载才能使其正常工作。 (don't forget to install uuid first through npm and call it). (不要忘了先通过npm安装uuid并调用它)。 Check out this link and you'll figure out what other payload props you need to pass. 查看链接,您将确定需要传递哪些其他有效载荷道具。

Dan's answer explains how to use third party push library, not FCM itself. Dan 的回答解释了如何使用第三方推送库,而不是 FCM 本身。

I've imported OneSignal's public and private VAPID keys into Firebase project/Project settings/Cloud Messaging/Web Configuration/Web Push certificates.我已将 OneSignal 的公共和私有 VAPID 密钥导入 Firebase 项目/项目设置/云消息传递/Web 配置/Web 推送证书。

Then in OneSignal's CSV export found test token in 'identifier' column, ie https://fcm.googleapis.com/fcm/send/SOME_TOKEN然后在 OneSignal 的 CSV 导出中找到了“标识符”列中的测试令牌,即https://fcm.googleapis.com/fcm/send/SOME_TOKEN

Then tried sending the notification using:然后尝试使用以下方式发送通知:

curl --ssl-no-revoke -X POST -H "Authorization: key=SERVER_KEY" -H "Content-Type: application/json" -d "{\"to\": \"SOME_TOKEN\", \"notification\": { \"title\": \"Hello\", \"alert\": \"World\", \"icon\": \"https://example.com/icon.jpg\", \"image\": \"https://example.com/icon.jpg\", \"custom\": { \"i\": \"00000000-0000-4000-8000-000000000000\", \"u\": \"https://example.com\" } }}" "https://fcm.googleapis.com/fcm/send"

But it returns "MismatchSenderId" error.但它返回“MismatchSenderId”错误。

Also tried updating push subscription to make use of exported columns web_auth and web_p256: https://developers.google.com/instance-id/reference/server#update_push_subscriptions but always getting "Request contains an invalid argument" error.还尝试更新推送订阅以使用导出的列 web_auth 和 web_p256: https://developers.google.com/instance-id/reference/server#update_push_subscriptions但总是收到“请求包含无效参数”错误。

What am I missing?我错过了什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何才能等到所有 FCM 订阅完成 - How can I wait until all FCM subscriptions are done 是否可以将 GCP JWT 令牌的有效期提高 1 小时以上? 是否有一个全局 API 端点,我可以 scope JWT 到? - Is it possible to increase the validity of GCP JWT tokens beyond 1 hour? And is there a global API endpoint that I can scope the JWT to? 如何导入 firebase 应用程序作为浏览器模块导入? - How can I import firebase app as browser module import? 我可以将我的 MySQL 数据导入 Google Spanner 吗? - Can I import my MySQL data into Google Spanner? 我们可以使用 Application Client ID + Client Secret 而不是 Tokens - Can we use Application Client ID + Client Secret instead of Tokens 无法为我的应用程序(SDK Android 5.0)的通知添加 Firebase FCM。 为什么? - Can't add Firebase FCM for notifications on my app (SDK Android 5.0). Why? Android 13. 使用FCM是否需要添加POST_NOTIFICATIONS权限? - Android 13. Do I need to add POST_NOTIFICATIONS permission when use FCM? 如何更新 FCM 令牌 - How to update FCM token FCM通知退订 - FCM notification unsubscribe 通过 API 向 iOS 发送 FCM 消息时无法更改徽章编号 - Can't get badge number to change when sending FCM message to iOS through API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM