Ios message push details, ios message push details
Message push process
The mobile phone system keeps communicating with Apple APNs.
When the App is opened, a toKen is obtained from the apple Apns.
Send toKen to the backend server
The server sends this toKen and pushes data to Apple APNs (the push channel is the only channel maintained by Apple)
After Apple APNs receives the message, it sends the message based on the toKen to our ios device.
After we get the toKen, we will know the push content of the APP.
Message push (V8.0 +For example) (build a push environment)
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
// Read the system version and determine whether the value is greater than 8.0
If ([[[UIDevice currentDevice] systemVersion] floatValue]> = 8.0 ){
// First, ask the user for the push permission and apply for the permission from the user
UIUserNotificationSettings * setting = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories: nil];
[[UIApplication sharedApplication] registerUserNotificationSettings: setting];
} Else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes :( UIRemoteNotificationTypeBadge | uiremotenotiftypetypesound | UIRemoteNotificationTypeAlert)];
}
}
// Callback after the user agrees to register the DeviceToken
-(Void) application :( UIApplication *) application didRegisterUserNotificationSettings :( nonnull UIUserNotificationSettings *) notificationSettings {
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
// Callback after successful registration to get the DeviceToken
-(Void) application :( UIApplication *) application didRegisterForRemoteNotificationsWithDeviceToken :( NSData *) aDeviceToken {
DLog (@ "succeed regist remote notification ");
NSString * tokenStr = [NSString stringWithFormat: @ "% @", aDeviceToken];
TokenStr = [tokenStr stringByReplacingOccurrencesOfString: @ "" withString: @ ""];
TokenStr = [tokenStr stringByReplacingOccurrencesOfString: @ "<" withString: @ ""];
TokenStr = [tokenStr stringByReplacingOccurrencesOfString: @ ">" withString: @ ""];
// Send to the server
}
// Callback for failed Registration
-(Void) application :( UIApplication *) application didFailToRegisterForRemoteNotificationsWithError :( NSError *) error {
DLog (@ "failed regist remote notification % @", error );
}
// When remote notification callback is received
-(Void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo {
NSDictionary * apsDictionary = [userInfo objectForKey: @ "aps"];
// Parse JSon
/*
Specific processing...
If (...){
Jump to a controller...
}
*/
}
Message push (local push)
@ Property (nonatomic, retain) UILocalNotification * localNotification;
-(Void) backGround {
_ LocalNotification = [UILocalNotification new];
// How long will the user be prompted?
_ LocalNotification. fireDate = [NSDate dateWithTimeIntervalSinceNow: 5];
_ LocalNotification. alertBody = @ "you 've been away for five seconds ";
_ LocalNotification. alertTitle = @ "your sister's! ";
_ LocalNotification. userInfo = @ {@ "key": @ "value "};
[[UIApplication sharedApplication] scheduleLocalNotification: _ localNotification];
}
-(Void) foreGround {
[[UIApplication sharedApplication] cancelLocalNotification: _ localNotification];
}
-(Void) viewDidLoad
{
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (backGround) name: UIApplicationDidEnterBackgroundNotification object: nil];
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (foreGround) name: UIApplicationWillEnterForegroundNotification object: nil];
}