简体   繁体   English

注册电容器推送通知

[英]Registering for capacitor Push Notifications

I am trying to set up Push Notifications for my React Ionic project via Firebase.我正在尝试通过 Firebase 为我的 React Ionic 项目设置推送通知。 I have done everything based on the documentation and I am able to receive push notifications both in foreground/background and in both IoS & Android.我已经根据文档完成了所有工作,并且能够在前台/后台以及 IoS 和 Android 中接收推送通知。

Below is the snippet I use to register:下面是我用来注册的片段:

PushNotifications.requestPermissions().then(result => {
            console.log(result)
            if (result.receive === 'granted') {
                // Register with Apple / Google to receive push via APNS/FCM
                PushNotifications.register();
            } else {
                // Show some error
            }
        });

My question is;我的问题是; where should this code live?这段代码应该放在哪里?

  • Does it need to be on my App component and thus being called every singe time it is re-rendering?它是否需要在我的 App 组件上,因此每次重新渲染时都会被调用?
  • Does it need to be in the index.js component and thus (I assume) only runs when the app is reloading?它是否需要在 index.js 组件中,因此(我假设)仅在应用程序重新加载时运行?
  • Do I need to register once and then keep checking if the device is still registered?我是否需要注册一次,然后继续检查设备是否仍然注册? If yes, how do I do so?如果是,我该怎么做?
  • Something else I am missing?我还缺少什么?

I have the feeling that I am missing something obvious here, but I cannot pinpoint what.我觉得我在这里遗漏了一些明显的东西,但我无法确定是什么。 Everything works fine, I just want to make sure I have a good understanding on how this is supposed to work in order to get it right.一切正常,我只是想确保我对它应该如何工作有一个很好的理解,以便让它正确。

Thanks in advance for any responses, I am just trying to get my head around how things work/should work and the documentation isn't doing the trick.提前感谢您的任何回复,我只是想弄清楚事情是如何工作/应该如何工作的,而文档并没有起到作用。

PS I would love if someone could also explain the differences/benefits between capacitor/push-notifications and ionic-native/fcm . PS如果有人也可以解释电容器/推送通知ionic-native/fcm之间的差异/好处,我会很高兴。 P.S2 Any kind of resources that explain these things in detail would be very welcome. P.S2 任何详细解释这些事情的资源都将受到欢迎。

PushNotifications.requestPermissions() is the function that opens the iOS system prompt that asks the user "Do you want to receive notifications from this app?" PushNotifications.requestPermissions()是 function 打开 iOS 系统提示,询问用户“你想从这个应用程序接收通知吗?”

For this reason, you must carefully choose when to call it.因此,您必须谨慎选择何时调用它。

If you put it in index.ts , for example, then the user will be immediately prompted to enable push notifications as soon as they open your app for the first time.例如,如果你把它放在index.ts中,那么一旦用户第一次打开你的应用程序,就会立即提示他们启用推送通知。 Many users do not want to give permission to send notifications as soon as they install an app (they want to check out the app first), so if you do it this way, you will probably have more users block your notifications.许多用户不想在安装应用程序后立即授予发送通知的权限(他们想先检查应用程序),因此如果您这样做,您可能会有更多用户阻止您的通知。

If your app has an onboarding (getting started) flow, one normal pattern is to have a step in onboarding that lists the benefits of getting notifications from your app and then provides a "next" link which calls PushNotifications.requestPermissions() , which will prompt the user to enable notifications.如果您的应用程序具有入职(入门)流程,一种正常的模式是在入职过程中列出从您的应用程序获取通知的好处,然后提供一个调用PushNotifications.requestPermissions()的“下一个”链接,这将提示用户启用通知。

Note that you can only ever ask users on iOS ONCE to enable notifications.请注意,您只能要求 iOS ONCE 上的用户启用通知。 So no matter how many times you call this function, your app only gets one chance.所以无论你调用这个 function 多少次,你的应用程序只有一次机会。 That's why it is so important that you call it at a timing when the user is likely to say yes.这就是为什么在用户可能会说是的时候调用它是如此重要的原因。

At present, none of this is relevant to Android because Android enables notifications for apps by default.目前,这些都与 Android 无关,因为 Android 默认启用应用程序通知。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM