Reasons for writing: I have read many articles pushed online, but none of them have perfect answers to my doubts. There are two main points: 1. Push is coming. I click the application icon to enter the application. 2. How to ensure that remote push messages can be obtained, because some remote push messages are very important? The main thing here is to verify which callbacks receive the content, and what is the behavior (a message pops up at the top of the phone and I call it behavior)

Actually received to push into application has so five scenarios, 1: the program runs in the foreground to push, 2: the program runs in the background to push, I clicked on push messages into the program, 3: the program runs in the background to push, I click the application icon to enter the program, 4: program didn’t run to push, I clicked on push messages into the program, 5: The program is not running to push, I click the application icon to enter the program

Speak to push the article is too much, I will not wordy principle (http://www.jianshu.com/p/ace1b422bad4) and configuration (http://www.jianshu.com/p/db9c95c9e29f); I will directly take my project (project minimum iOS8) from the following three aspects to answer the above five situations in which the content will be received in the callback, what is the behavior

One: local push

If we look at UIApplicationDelegate to see which callbacks are related to local push, we can find the following three; Let’s say you don’t know what B and C mean at this point. I’ll remember later, but I don’t want to delete them:

- (void) application: (UIApplication *) application didReceiveLocalNotification notification: (UILocalNotification *) {} / / A callback  (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier ForLocalNotification :(UILocalNotification *)notification completionHandler:(void(^)())completionHandler {}//B callback - (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo CompletionHandler (void (^) ()) completionHandler {} / / C callbackCopy the code

If you want to call XXX instead of A, you should call XXX instead. Dude, this function is valid for iOSx-iOS10, you panic, wait for iOS11 to come out again; And then B and C are described the same way and the same way, we’re going to use B, C is iOS9 to iOS10, B is iOS8 to iOS10 and we’re going to adapt the version; We don’t know yet when B and C are called, but it doesn’t matter. Let’s take our time and experiment. We’ll write A and B to appdelegate.m; And then we’ll do it separately

1: write both A and B callbacks

1) The application runs in the foreground to push locally

I create a button in my project and add a local push to the button response that only fires once. Now we run the program, click the button and let the program run to the foreground, slowly waiting for the local push to trigger

The breakpoint is executed as planned. First, the A callback is executed. Let’s print notification below XCode to see what is going on:

{fire date = Monday, October 24, 2016 4:03:08 CST, time zone = Asia/Shanghai (GMT+8) offset 28800, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = (null), user info = { action = GENERAL; "company_no" = 10295; content = "\U4e8b\U52a1\U63d0\U9192: boot1"; "from_user_no" = 21175; "target_id" = 15812; time = 1477296248000; "to_user_no" = 21175; type = CALENDARTIP; unread = 1;Copy the code

The content is approximately the trigger time (Fire date) + time zone (time zone) + interval (repeat interval) + repeat count (repeat count) + next fire date (next fire date) + content added to the local push (user) Info), ok, no problem, then we continue to execute the program, why B does not execute? Let’s go back to the comment above the B callback:

// Called when your app has been activated by the user selecting an action from a local notification.

// A nil action identifier indicates the default action.

// You should call the completion handler as soon as you've finished handling the action.Copy the code

This callback is triggered when the user clicks on an action from the local notification, an action that is not nil indicates the default action, and you should write to complete the callback when you hold the action (I’ve been overwhelmed by my English); Click on an active item from the notification bar. What is an active item? Does the backend go into the foreground or does the killed project start? Anyway, the conclusion is important:

Which callbacks will trigger: The application in the foreground will only trigger the A callback

What behavior: No behavior

2) The program is running in the background to push locally. I click push message to enter the program

Again, we click on the button and we run it in the background; Make a breakpoint between the two callbacks and wait for the local push to trigger

Ok, now a message pops up at the top of the phone and a number appears in the upper right corner of the app icon. Notice that neither of the callbacks is triggered, like this:




There is a notice in the notification bar




App ICONS have numbers

Ok, let’s click on the notification bar excitedly. Ok, the A callback is triggered first. Let’s print notification:

{fire date = Monday, October 24, 2016 4:25:21 PM CST, time zone = Asia/Shanghai (GMT+8) offset 28800, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = (null), user info = { action = GENERAL; "company_no" = 10295; content = "\U4e8b\U52a1\U63d0\U9192: boot3"; "from_user_no" = 21175; "target_id" = 15815; time = 1477297581000; "to_user_no" = 21175; type = CALENDARTIP; unread = 1;Copy the code

This is the same as the above, no explanation, we continue to run, ok, B is not triggered, conclusion:

Which callbacks are triggered: Clicking the notification bar to enter the foreground from the background only triggers the A callback

What behavior: ICONS with numbers, notifications with notifications

3) The program is running in the background and pushed locally. I click the application icon to enter the program

Repeat the steps above, click on the button to start the application in the background, set a breakpoint between the two callbacks, and slowly wait for the local push to trigger

Ok, now a message pops up at the top of the phone and a number appears in the upper right corner of the app icon; Note that neither of the callbacks is triggered, as above, and then we click on the app icon to enter the app:




Click the app icon to enter the foreground

Ok, you read correctly, neither A nor B trigger, so how do I get local push messages, are you kidding me? Conclusion:

Which callbacks will be triggered: Click the application icon to enter the foreground from the background. Callback A and callback B will not be triggered

What behavior: ICONS with numbers, notifications with notifications

4) The program did not run to local push, I click push message to enter the program

I was wondering how to break this point, because the program is not running… Let’s use a UIAlertView to display the Notification. Here we start the program:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {}Copy the code

Here also show the launchOptions, because the application should launch with data; Similarly, click the button to add a local notification and then kill the application, waiting for the local push to trigger

Ok, the notification from the top of the phone will arrive as scheduled, and the callback will definitely not trigger at this time. Let’s click on the notification bar action to start the application:




Launch parameters

Sure enough, the startup parameters included our push content; We click Cancel, and the prompt boxes of A and B do not pop up. Conclusion:

Which callbacks will be triggered: If the program is running, click the notification bar to start the program, callbacks A and B will not be triggered, and the startup parameters have push content

What behavior: ICONS with numbers, notifications with notifications

5) The program did not run to local push, I click the application icon to enter the program

Still, we enter the application and click the button to push, and then kill the application and wait for the push; When the push arrives, the two callbacks are not triggered. Click on the app icon to enter the application and you will see the following effect:




Launch parameters

Click Cancel to continue the operation, and then the prompt box of A and B does not pop up. Conclusion:

Which callbacks will be triggered: The program is not running, click the application icon to start the program, the A and B callbacks will not be triggered, and the startup parameters have no content

What behavior: ICONS with numbers, notifications with notifications

Okay, doesn’t that hurt? “A” and “B” together never trigger “B”; So let’s see if we can just write one of these functions (I’m still trying to figure out what B does at this point).

2: Write only A callback

The verification method is the same as above, I will not screenshot (but I really repeat oh), directly write the conclusion

1) The application runs in the foreground to push locally

Which callbacks will trigger: The application in the foreground will trigger the A callback

What behavior: No behavior

2) The program is running in the background to push locally. I click push message to enter the program

Which callbacks will be triggered: The application is in the background, and clicking the Notification bar to enter the foreground will trigger the A callback

What behavior: ICONS with numbers, notifications with notifications

3) The program is running in the background and pushed locally. I click the application icon to enter the program

Which callbacks will be triggered: If the application is in the background, clicking the application icon to enter the foreground will not trigger A callback

What behavior: ICONS with numbers, notifications with notifications

4) The program did not run to local push, I click push message to enter the program

Which callbacks will be triggered: The program is not running, click the notification bar to start the program, the A callback will not be triggered, the startup parameter has content

What behavior: ICONS with numbers, notifications with notifications

5) The program did not run to local push, I click the application icon to enter the program

What callbacks will be triggered: The program is not running, click the application icon to start the program, the A callback will not be triggered, and the startup parameters will not be content

What behavior: ICONS with numbers, notifications with notifications

Ok, same as above, so we’ll just write the B callback now to verify

3: Write only the B callback

I am a little excited that I can finish my local push soon. Similarly, to come to the conclusion (I still don’t remember what B is)

1) The application runs in the foreground to push locally

Which callbacks will trigger: The application is in the foreground and does not trigger the B callback

What behavior: No behavior

It didn’t trigger. I couldn’t believe my eyes, so I tested it again, and it didn’t trigger

2) The program is running in the background to push locally. I click push message to enter the program

Which callbacks will trigger: The application is in the background, and clicking the notification bar to enter the foreground will not trigger the B callback

What behavior: ICONS with numbers, notifications with notifications

3) The program is running in the background and pushed locally. I click the application icon to enter the program

Which callbacks will be triggered: If the application is in the background, click the application icon to enter the foreground, the B callback will not be triggered

What behavior: ICONS with numbers, notifications with notifications

4) The program did not run to local push, I click push message to enter the program

Which callbacks will be triggered: The program is not running, click the notification bar to start the program, the B callback will not be triggered, the startup parameter has content

What behavior: ICONS with numbers, notifications with notifications

5) The program did not run to local push, I click the application icon to enter the program

What callbacks will be triggered: The program is not running, click the application icon to start the program, the B callback will not be triggered, and the startup parameters have no content

What behavior: ICONS with numbers, notifications with notifications

Okay, now it’s time to Google what function B does, and why it doesn’t fire; I found the answer here, http://www.bozhiyue.com/ios/2016/0811/358999.html, well, I finally know what it means. The B function is triggered by clicking here:




This is action. Oh, my God

So let’s rearrange the results (ok, this is the right time),

1) The application runs in the foreground to push locally

Which callbacks are triggered: The application is in the foreground and the B callback is not triggered (because no action was clicked)

What behavior: No behavior

2) The application is running in the background to push the local message, I click the action to push the message

Which callbacks will be triggered: The program runs in the background, push action will not go to the foreground, B callback will not be triggered

What behavior: ICONS with numbers, notifications with notifications

Still no trigger, I go; What the hell is this? Can we be less routine, we continue to Google; http://www.jianshu.com/p/803bfaae989e here said to remote push can trigger, the local delivery not trigger? And I also added a custom action:




The custom action

What exactly does that mean? Let’s go ahead and experiment to see if the remote push triggers

3) The program is running in the background and pushed locally. I click the application icon to enter the program

Which callbacks will be triggered: The application is in the background, and clicking the application icon to enter the foreground will not trigger the B callback (because no action was clicked).

What behavior: ICONS with numbers, notifications with notifications

4) The application is not running to push the local message, I click the action to push the message

Which callbacks will be triggered: The application is not running, clicking push action will not start the application, and the B callback will not be triggered

What behavior: ICONS with numbers, notifications with notifications

5) The program did not run to local push, I click the application icon to enter the program

Which callbacks will be triggered: The application is in the background, and clicking the application icon to enter the foreground will not trigger the B callback (because no action was clicked).

What behavior: ICONS with numbers, notifications with notifications

Well, I’ll have to write another article about how to customize push action and the corresponding behavior of the above 5 cases, but I probably didn’t add it correctly because I didn’t trigger it;

Now let’s look at remote push. This time, we can finally figure out what A and B do. So when we do a local push, we write both callbacks, because they’re different.

Two: remote push, do not use a third party

Remote push must be real, remind! Let’s start by looking for remot-push related callbacks in UIApplicationDelegate (we’ll leave those related to actions and write another article on actions) :

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {}//B

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {}//CCopy the code

Then B callback https://docs.kii.com/cn/guides/cloudsdk/ios/managing-push-notification/push-to-user/receiving-messages/ has explained is push the use of silence B callback, so what is silent push? http://www.devlizy.com/ios-ding-shi-huo-qu-he-jing-mo-tui-song/ offers an explanation here, that is to say, your program in the background received remote push directly triggered the callback will be without such application into the foreground, so funny, But we’re not going to test that; C callback is to receive remote push, C callback annotation indicates that use B to receive silent push, use XXX to receive remote push, we do not care; So we’ll just add C to appdelegate. m and a callback will look like this:




Remote push callback

Since our project has integrated a third push, we do not need to use the third push to test. We will use the following tools to test the environment simulation push (you don’t need to download, just watch me perform).




The test environment simulates remote push

After we download and install this tool, we will see this interface when we start it:




Analog push tool

How to use it is not said, so simple; We get the device token first, and then we get the following error when we get it:




Failed to obtain the token

To Google, found http://blog.csdn.net/u013263917/article/details/24712797 and http://blog.csdn.net/soindy/article/details/46537095, So I re-generated the developer certificate, push certificate and description file on the official website. It’s the same question; I wonder if it’s because my project integrated a tweet and I turned it off; It’s the same question (I’m a little flustered at the moment); Then said open here: http://www.jianshu.com/p/c2bb07786fd1




Open the push

The red error doesn’t have to be solved, and then you can see here:




Testing is not the same as being formal

At this point, the device token can be obtained:




You can get the token

Start verifying with a tool that simulates push!

One: Test the C callback

1) The application is running in the foreground to push it remotely

Start the real machine, then we click here to simulate push (use “push” button instead of description below) :




Waiting for; Then our program pops up, and the content is what the tool simulates. Conclusion:

Which callbacks are triggered: C callbacks are triggered when the application is in the foreground

What behavior: No behavior

2) The program is running in the background to push it remotely. I click push message to enter the program

Switch the application to the background, click the “push” button of the tool, and wait for the push; Click push to enter the foreground and a prompt box pops up. Conclusion:

Which callbacks will be triggered: The program runs in the background, and clicking push will bring it to the foreground, triggering the C callback

What behavior: ICONS with numbers, notifications with notifications

3) The program is running in the background to push it remotely. I click the application icon to enter the program

Still, switch to the background, click the tool “push” button, and wait; No popover, conclusion:

Which callbacks are triggered: The application is running in the background and clicking the application icon brings it to the foreground. The C callback is not triggered

What behavior: ICONS with numbers, notifications with notifications

4) The program did not run to remote push, I click push message to enter the program

We kill the program, click the tool “push” button, and wait; Click the push bar to enter the program, the startup parameter is displayed, and the callback of A is not triggered. Conclusion:

Which callbacks will be triggered: The program is not running, click push to enter the foreground, C callback will not be triggered, and the startup parameters have content

What behavior: ICONS with numbers, notifications with notifications

5) The program did not run to remote push, I click the application icon to enter the program

Kill the program, click the “push” button, and wait; Click the icon to start the program, no startup parameter, no C callback is triggered, conclusion:

Which callbacks will be triggered: The program is not running, click the application icon to enter the foreground, the C callback will not be triggered, and the startup parameter has no content

What behavior: ICONS with numbers, notifications with notifications

This is consistent with local push, but have you found that native remote push can not ensure that users can get data, because the application icon into the program is not, so only the server to save the client into the application pull is not? So is the third party push done? We’re looking forward to testing it out

Three: remote push, use a push and other third parties

Also, we do not speak how to configure a on http://docs.getui.com/mobile/ios/overview/; After integration, the callback for the push to receive the push will look like this (you don’t need integration, just watch me) :

– (void)GeTuiSdkDidReceivePayloadData:(NSData *)payloadData andTaskId:(NSString *)taskId andMsgId:(NSString *)msgId andOffLine:(BOOL)offLine fromGtAppId:(NSString *)appId {}//A

Add the display code to the callback:




A back into the

We’ll also add native remote push to see if it shows up as well:

– (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {}//B

Again, we launch the app and have the background send us one tweet after another

1) The application is running in the foreground with a push

Waiting for push; The “push A” callback is not executed.

Which callbacks will trigger: The application in the foreground will trigger A push A callback, not the native B callback

What behavior: No behavior

Here we can think of it as a push-over, regardless of the original thing, let’s take it this way

2) When the program is running in the background, I click push message to enter the program

Then, we switch the application to the background and wait for push; At this time, we click push to enter the foreground, the push A is displayed, and then the native B also pops up, let’s see what the native content is:




The native is triggered

This content is not what I asked the background to send, it should be a push side additional, so the conclusion:

Which callbacks are triggered: The application runs in the background, and clicking Push brings it to the foreground, triggering A push A callback, and then the native B callback

What behavior: ICONS with numbers, notifications with notifications

3) When the program is running in the background, there is a push. I click the application icon to enter the program

Still, switch to the background and wait for push; Click the application icon to enter the program, push A trigger, native B does not trigger, conclusion:

Which callbacks will be triggered: The application is running in the background, and clicking the application icon will bring it to the foreground, triggering A push A callback, but not the native B callback

What behavior: ICONS with numbers, notifications with notifications

This is better, the app icon will push content, this is the function I want

4) The program is not running and there is a push message. I click push message to enter the program

Kill the program, waiting for push; Click push to enter the program, the start parameter has content but not the content I asked the background to send (also additional content), then it will trigger A push A callback, will not trigger the native B callback, conclusion:

What callbacks will be triggered: the program is not running, click push to enter the program, the startup parameters have content, trigger A push callback, do not trigger the native B callback

What behavior: ICONS with numbers, notifications with notifications

Additional content is easy to understand, just alert you to a message and then send you the actual content when you are entering

5) The program is not running and there is a push. I click the application icon to enter the program

Still kill the program, waiting for push; Click the application icon to enter the program. There is no content in the startup parameters. Push A is triggered, but native B is not triggered.

What callbacks will be triggered: the program is not running, click the icon to enter the program, start no content, trigger A push A callback, do not trigger the native B callback

What behavior: ICONS with numbers, notifications with notifications

Well, third-party push ensures that the message will be delivered to the user, which is good, you might say, how does a tweet do that? Here’s a website to check out

Four: conclusion

English is really a good thing, before doing things to read more articles, do not understand more verification

One: use local push

1: Push content will be retrieved in startup parameters or local push callback. 2: application icon entry program will not get any content

Two: use native remote push

1: push content will be obtained in startup parameters or remote push callback; 2: no content will be obtained when the application icon enters the program; 3: the server should save push content to avoid the loss of important data

Three: use a tweet three push

1: the user can definitely get push content from push-back in the program; 2: the original remote push back and startup parameters are additional content and need not be handled

Five: trivia

First, I was so confused that I couldn’t remember what the following callback was for, and it took me all morning to figure it out:

-(void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification CompletionHandler :(void(^)())completionHandle{}// the custom push action is clickedCopy the code

Then, my previous push test was conducted in the test environment without any problems. Then the next day, I went to the formal environment and found that the performance was different. I got a little panicked and immediately added QQ, the service provider of push, and finally understood. Is the problem of his own certificate at http://www.qingpingshan.com/rjbc/ios/139338.html; And finally figured out why the company signed up for two of the same apps on its twitter page:




Two of the same applications

This is a test environment, a formal environment!