Ios message push details, ios message push details

Source: Internet
Author: User

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];

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.