Step-by-Step Guide to Creating Phone Notifications
Creating Phone Notifications

Step-by-Step Guide to Creating Phone Notifications

If you're building a project or home application that uses mailing, you'll need to learn how to set up notifications for users. This is especially true for mobile versions of applications.?

Let's talk about push notifications on mobile devices and the challenges you might face while implementing them.

In the beginning, we need to decide what goals we want to achieve.

? First of all, we need to receive phone notifications on three types of devices: Android, iOS, Huawei.

? Secondly, we need to receive feedback on the delivery of notifications. Because it is important for us to know who received our information in order to activate other notification channels.

? Thirdly, to show notifications in three phone states: background, foreground and when the application is unloaded from memory. In short, we want to get the maximum percentage of delivery.

So, let's see what are the differences between the platforms.

Android and iOS have their own notification services. We are lucky that we can use the Google Firebase service to achieve our first goal. The fact is that Google Firebase allows you to send messages for both platforms. It helps us set up and send messages using the API.?

?? At first I used the old API and in the end I had to use v1 API. FCM (Firebase Cloud Messaging) is encouraged to use and explain all the benefits in their documentation.

The best benefit for me is that the API allows you to separate platform messages in a single request. If, like me, you want to use different fields for the platform, you don't need to create two queries. As a result, your message will look like this:

URL: https://fcm.googleapis.com/v1/projects/project_id/messages:send?

BODY example:??

FCM

Since I needed to test it from the frontend, I ran into a problem.?

?How can I get an authorization token?

After some research I found the solution! This article describes the solution that worked for me.

You can use the RN Firebase library for push notifications. It works for both platforms and also has other features such as analytics and crash logs.

The Huawei App Gallery has its own push kit. All you need is to install a push kit? for RN and test it on a Huawei device.?

?? It is possible that some functions will work correctly only when using a Huawei device. If you need to use HMC core for testing on Xiaomi or other devices (non-Huawei), please test push kits on their cloud messaging platform in Gallery.

The Huawei push notification kit provides everything we need: delivery status, opening an application or a web page on a click. All these functions can be configured from the API side.

URL: https://push-api.cloud.huawei.com/v1/project_id/messages:send?

BODY example:??

HMC

Notification settings

As you remember, one of our goals is to get the maximum percentage of delivery. The documentation says that for this you need to use the "notification" field in the request body. This means that we were sending notifications instead of data push messages. Data push does not guarantee that you will receive the message. Although, if we receive a data message, we need to show it to the user.

?? Use notification messages when you want the FCM SDK to handle displaying a notification automatically when your app is running in the background. Use data messages when you want to process the messages with your own client app code.

We need to show that the PN (push notifications) event phone does not open our application, so I chose the notification message as required.

If we use it on Android, we can get the delivery status and can inject HTTP requests into the message handler.

For the foreground:

Or when app in background or unloaded from memory:

This handler works for both platforms. But I'm having issues with iOS and I chose not to call it for iOS. (Later we will learn why I put such a condition). I? use notifee to show local push notification because Android doesn't show it by default.

The problems started on iOS. Without "notification" files, iOS sometimes could not call the code of the RN index.ts file and requests were not sent.

The developer of the FCM library says the following about this:

?? ..Background message delivery on iOS is not guaranteed, by design, by Apple in defense of battery life on their platform.
?? If you are supplying the content-available flag set to 1 in the JSON, and the device has enough power, and the device is not overheated, and the operating system thinks you have used the app enough that the user wants to spend battery on it, and background refresh is enabled for the app, and power saving is not turned on for the device, then the message may deliver in the background on iOS.
?? You can watch the iOS logs to see the decision making process the operating system does as it decides whether to deliver the message or not.

Based on the prevailing conditions, I found a solution - the customer.io library. But it only has the push function in the paid version. And since this is the only thing we need from everything that they offer, we refused it.

Notifee docs have explanations on how to integrate the push notification extension, but they offer Objective-C code. It's too complicated to understand and is considered obsolete. So, after several attempts, I decided to create an extension file using swift. I was able to find some information about a quick http request and paste the code into the extension. I received several responses from the iOS community and got what I needed. This allows you to make http requests after push notifications have been delivered to the platform.

?? Recompile project with new code!

Globally, this extension allows you to do something within 60 seconds before a push notification appears. At the end, we can get feedback when the notification is delivered.?

? Sergey Kuharyonok


要查看或添加评论,请登录

Start Matter的更多文章

社区洞察

其他会员也浏览了