This article aims to conduct a complete analysis of iOS push. If you know nothing about push before, you will become a push veteran after reading the full text carefully, and you will have a full understanding of various details and principles. The following are some experiences of pikacode using iOS push. Welcome to communicate with each other and point out the mistakes and omissions.
Push service can be said to be the standard configuration of all apps. No matter which type of app is, pushing largely determines the app's opening rate, usage rate, and survival rate. Therefore, being familiar with and mastering the principles and methods of push is a necessary skill for every developer and is crucial for every company that relies on apps.
From the newly added UserNotifications Framework in iOS 10, we can see that Apple integrates the original scattered API and adds many powerful features. From the official perspective of Apple, it is bound to attach great importance to the impact of push services on Apps and their impact on the long-term development of the Apple iOS ecosystem.
Preparation
Tip 1:Push Notification must be purchased for Apple developer accounts and use specific push certificates
You cannot push it with a free account.
What if we are using a third-party push service (hereinafter referred to as a third-party)? For example, "Aurora Push". You must also purchase a developer account. Because all third parties will send push requests to APNs (Apple Push Notification service), all pushes are issued by APNs.
How to register and configure the certificate correctly.
Principles
Tip 2:Push notifications themselves are the behavior of the iOS system, so when the app is not running (not in the foreground or in the background):
It can still be pushed and received (notification center notifications, top banners, small dots in the upper right corner of the refresh App, namely badge [hereinafter referred to as corner marks], etc. will be controlled and displayed by the system).
When you receive a push, you cannot get the notification content in the App code. Because of the sandboxing mechanism, no code in the App can be executed at this time.
Tip 3:Register a push service with APNs on your mobile phone
1. Register the push service in the code:
#ifdef __IPHONE_8_0 if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge| UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; } else { UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; } #else UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; #endif
2. When this code is triggered for the first time, there will be a system pop-up window asking if you allow the app to push information to you. When you choose to allow, the system will package the App+mobile unique ID+certificate information to the APNs server to register the push service. The APNs system will verify whether the app installed on the phone has push permissions. Therefore, you must join the Apple Deveice mobile phone and use the push certificate of the corresponding app to successfully register.
3. If the registration is successful, you can get the deviceToken in the following method. It is a unique identifier for the combination of the mobile phone + the App. When using remote push, you only need to send the push message to the specified deviceToken to communicate the push information to the specified app of the specified mobile phone. Therefore, if you use a third party, you need to pass the deviceToken to the third party in this method. (In iOS 9, in order to better protect user privacy, multiple repeated deletion/installation of the App will cause deviceToken to change continuously. Sometimes there will be a problem that a push phone will receive 2 times, which is an iOS 9 system problem).
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [JPUSHService registerDeviceToken:deviceToken];//Transfer deviceToken to Aurora Push }
4. If all the above steps are successful, you can obtain the device registration id provided by a third party. Whether the id value can be obtained can be used as a criterion for determining whether the device can successfully push (see Tip 6 - Registration ID). Because when you get the value, it must be:
The push certificate is configured correctly (you have push permissions).
The device successfully registered with APNs and returned the deviceToken (APNs can recognize your device).
The returned deviceToken is passed to the third party, and the unique identifier registration id is successfully generated on the third party (the third party can pass your device information to APNs).
5. In summary, registration and receiving push must be done using real machines and must be connected to the Internet.
Tip 4:The process of pushing notifications from server --> App code
1. Use your company or third-party server to send push requests to APNs (please refer to Apple APNs related information, or third-party push provides a simpler REST API).
Receive and verify push requests.
Find the device and send a push.
4. When the mobile phone receives push notifications, the system processes them according to the status of the app:
Received by the front desk:
The system will pass the notification content to didReceiveRemoteNotification
Received in the background:
If Remote Notification is enabled, the system will push it to didReceiveRemoteNotification:fetchCompletionHandler: (see Tip 5 - Background Push), otherwise the push will not be received in the code at this time.
Display banners, notification centers, sounds, corner signs.
Exit Received:
If you click the Push Banner/Notification Center to start the App, the system will pass the notification to didFinishLaunchingWithOptions.
Display banners, notification centers, sounds, corner signs.
Push notification content
Tip 5:Push notifications are divided into 2 types: local/remote:
Local notification, you can specify the push time, and push notifications pop up on time at that time.
Remote push notifications, divided into three types: ordinary push/backend push/silent push. There is a delay problem (caused by instability in Tip 1 point 2, APNs and huge requests during peak hours).
Normal push
This is the push notification we usually see on our mobile phones.
Contains sound, banner, corner mark, custom fields.
App :
When you are in the front desk, the banner will not be displayed. You can get the notification content through didReceiveRemoteNotification (iOS 7 before) didReceiveRemoteNotification:fetchCompletionHandler: (iOS 7 after) (see here for the front desk to display the banner).
In the background, the banner will be displayed and the notification content cannot be obtained.
In exit, the banner will be displayed and the notification content cannot be obtained.
Click the icon to start, and the notification content cannot be obtained.
Click the notification banner to start and get the notification content in didFinishLaunchingWithOptions.
Notification contentSimilar to the following:
{ "_j_msgid" = 200806057; // Ids included with third-party to count clicks aps = { alert = "Show content"; badge = 1; // App angle marks, which can push n, +n, and -n to achieve fixed, increase, and decrease angle marks sound = default; // Push sound, the default system tri-tone. If you need to use your own sound, you need to drag and copy the sound file to any location in the Xcode project directory, and specify its file name when pushing }; key1 = value1; // Custom fields, multiple groups can be set, used to process internal logic key2 = value2; }
Backend push
Various display effects are exactly the same as ordinary push.
Must carry "content-available" = 1;
At least 1 field in alert, badge, sound must be carried.
Only supported in iOS 7.
This feature must be enabled in Xcode projects TARGETS - Capabilities - Background Modes - Remote notifications.
App:
In the foreground, you can get the notification content through didReceiveRemoteNotification (iOS 7 before) didReceiveRemoteNotification:fetchCompletionHandler: (iOS 7 after) .
In the background, you can get notification content through didReceiveRemoteNotification:fetchCompletionHandler: // Get the only difference between the situation and ordinary push. At this time, the iOS system allows developers to execute some code while the App is in the background, providing about a few minutes, which can be used to secretly refresh the UI, switch pages, download update packages, etc.
Exit, unable to obtain notification content.
Click the icon to start, and the notification content cannot be obtained.
Click the push banner to start and get the notification content in didFinishLaunchingWithOptions.
The notification content is similar to the following:
{ "_j_msgid" = 2090737306; aps = { alert = "Show content"; badge = 1; "content-available" = 1; // Must include fields sound = default; }; key1 = value1; }
Silent push
No display effect.
"content-available" = 1; must be carried, so silence must be in the background.
Alert, badge, sound must not be carried.
Custom fields are allowed.
App :
At the front desk,The notification content can be obtained through didReceiveRemoteNotification (iOS 7 before) didReceiveRemoteNotification:fetchCompletionHandler: (iOS 7 after).
In the background,You can get notification content through didReceiveRemoteNotification:fetchCompletionHandler: // Get the only difference between the situation and ordinary push. At this time, the iOS system allows developers to execute some code while the App is in the background, providing about a few minutes, which can be used to secretly refresh the UI, switch pages, download update packages, etc.
Exit,Notified content cannot be obtained.
The notification content is similar to the following:
{ "_j_msgid" = 3938587719; aps = { alert = ""; "content-available" = 1; // Must include fields }; key1 = value1; }
Push target
Alias, tags, and Registration IDs are all functions provided by third parties to more conveniently specify push targets.
Tip 6:Push can be divided into:
broadcast
Send to all users without discrimination.
alias push
Features provided by third parties
An app for a mobile phone can only set one alias (can be modified).
It is recommended to use a different alias for each user to determine a unique user (plural users can also use 1 alias).
During push, multiple alias can be specified to send the same content.
Only users who specify alias can receive pushes.
Tag tag push
Features provided by third parties.
Multiple can be set, can be added, and can be cleared.
Used to specify a variety of attributes, such as "1000" + "daily" + "discount" can be used to indicate that users who spend more than 1k per month, like to buy daily necessities, and prefer discounted products.
If you want to delete, you need to save the set tags to NSUserDefaults when you set them last time. After removing unnecessary tags this time, you can reset them.
During push, you can specify multiple tags to post the same content.
If the mobile phone sets any of the multiple tags specified by push, it can receive a push message. If you specify "1000" + "globe" + "original" (100 yuan consumer, global purchase, original price), then users who set "100" + "globe" + "discount" (100 yuan consumer, global purchase, discount price) can receive the push message.
Registration ID push
Features provided by third parties.
After the deviceToken is provided to a third party in step 3 of Tip 3, its server will automatically generate a unique id pointing to the phone.
You can specify multiple ids to send messages during push.
Can be used for accurate push to core users and flagship users.
In-app message
Tip 7:The difference between in-app messages (hereinafter referred to as messages) and push notifications, messages:
No Apple push certificate is required.
Delivered by a third-party server, not APNs.
It is faster and has little delay than notifications and can be used for instant delivery of IM messages.
It can retain offline messages for a long time and obtain all historical message content.
Send messages through long connection technology, so:
The phone must be started and connected to a third-party server.
If the phone starts and cuts to the background immediately, the connection is likely not established.
The phone must be in the front desk to receive the message.
When the phone switches back to the foreground from the background, the connection will be automatically re-established and an offline message will be received.
There is no display (banner, notification center, corner marker, sound), so you can:
Custom fields implement UI effects.
Handle the App internal logic completely in silence.
Use some functions that will not pass the App Store review. Turn off the function during the review, and then receive messages after it is launched to enable related functions.
Combination trick
Tip 8:Tags combination tips
See Tip 5 - Tag tag push.
You can statistically analyze user behavior on the server, and then send the specified tags to the mobile phone. After the mobile phone receives it, you can type the corresponding tags for the user.
Tip 9:Tip of notification + message combination
First, let’s look at the comparison of notification and message characteristics:
notify information
Delivery time There may be a few seconds delays
Getting time: Getting content can be obtained in the front desk or back desk.
Offline content is retained for a period of time, it will be discarded after expired, and historical content cannot be queried. Always retained, all historical content can be queried.
The system display will be displayed (silent push or the app is not displayed in the front desk)
Since there are differences in their respective characteristics, the combination of the two is an inevitable choice to maximize the performance of App push:
Scenario 1:
QQ/WeChat Chat. A set of notifications + messages will be issued at the same time. If the user does not start QQ, although there is a delay, he will definitely be able to receive the notification first. After receiving the notification reminder, the user opens the App and receives offline messages at this time, updates the UI immediately, and sends/receives messages with friends in real time. (After receiving the notification, disconnect the network and start the app, you will find that the content of the notification just now will not be displayed on your phone, because it relies on pulling messages to refresh the page, rather than not a notice that is not stable enough).
Scenario 2:(Looking forward to your addition...)
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.