How to handle iOS Push Notification
Handle iOS Push Notification
Handle push notification on iOS is quite hard if you don't understand it. In short topic, I on;y want to present how I handle when user tap on push notification when app is in active/background or killed.
First, these are few important notes that you need to keep in mind:
- APNS have sandbox & prod certificates, don't forget when you run the app in debug mode, you have to use sandbox.
- The registration token cannot be shared, sandbox token cannot be use for prod and vice versa.
Others tut:
- Apple Documents
- APNS with HTTP2
Generate Push Certificate:
cert_file="mobisys_prod_apns.cer"
key_file="mobisys_prod_apns.p12"
openssl x509 -in $cert_file -inform der -out pem_cert.pem
openssl pkcs12 -nocerts -in $key_file -out pem_key.pem
cat pem_cert.pem pem_key.pem > PushCert.pem
First, these are few important notes that you need to keep in mind:
- APNS have sandbox & prod certificates, don't forget when you run the app in debug mode, you have to use sandbox.
- The registration token cannot be shared, sandbox token cannot be use for prod and vice versa.
Others tut:
- Apple Documents
- APNS with HTTP2
Generate Push Certificate:
cert_file="mobisys_prod_apns.cer"
key_file="mobisys_prod_apns.p12"
openssl x509 -in $cert_file -inform der -out pem_cert.pem
openssl pkcs12 -nocerts -in $key_file -out pem_key.pem
cat pem_cert.pem pem_key.pem > PushCert.pem
Common Mistakes
- using wrong certificate (APNS will return error) / wrong passphrase (in script will retunr error)
- using invalid push token
Handle Push Notification
1) To handle iOS push notification easier, I usually have type for each of notification. I suggest the structure should be:
aps = {
alert = "";
badge = 0;
content-available = 1;
sound = default;
};
noti = {
type = 12;
type = 12;
};
*** content-available = 1; must be set if you want your app be able to handle push notification in background mode.
*** sound & alert should be set to empty if you want to send a silent notification (which no appear in phone notification and not show when app in background)
2) Application State is also needed. There're 3 states:
+ UIApplicationStateActive : app in foreground
*** content-available = 1; must be set if you want your app be able to handle push notification in background mode.
*** sound & alert should be set to empty if you want to send a silent notification (which no appear in phone notification and not show when app in background)
2) Application State is also needed. There're 3 states:
+ UIApplicationStateActive : app in foreground
+ UIApplicationStateInactive: app in the middle of active. like when user tap on push notification when your app is wake up
+ UIApplicationStateBackground: app in background, but not killed
Handle Receive:
+ happen when app state is: UIApplicationStateActive & UIApplicationStateBackground.
+ when app state in background, you have to call completionHandle after maximum 30 seconds, so, it's safe to put a timeout timer after 15-20 seconds to callback if your app process too long, otherwise iOS system might kill your app.
+ when app state in active: you process the notification, then call completion handle also, but no need to care about maximum timeout
Handle Tap:
+ happen when app state is: UIApplicationStateInActive
+ there're 2 cases to care:
1) If handle notification don't need extra info (e.g just show a message box), so, you can handle it independently.
2) If handle notification need extra info (e.g when your app is killed and you need user login first, or you need client fetch some information from backend). In this case, if safe to save all notification user-info to a global var then process it later when your app have enough information.
* Let say my method to handle is:
+ there're 2 cases to care:
1) If handle notification don't need extra info (e.g just show a message box), so, you can handle it independently.
2) If handle notification need extra info (e.g when your app is killed and you need user login first, or you need client fetch some information from backend). In this case, if safe to save all notification user-info to a global var then process it later when your app have enough information.
* Let say my method to handle is:
- (void)handleTapOnPushNotificationWithUserInfo:(NSDictionary *)userInfo
* My flow should look like this:
Comments
Post a Comment