iOS development three steps to fix Baidu push send

Source: Internet
Author: User

iOS development three steps to fix Baidu push send 

Baidu push is very simple, ready to work: In the Baidu Cloud push platform registration application, upload certificates.

Step One:

Baidu Cloud push Platform Http://push.baidu.com/sdk/push_client_sdk_for_ios

Download the iOS SDK package here, such as;

Drag the folders inside the SDK package into your project, such as the first step.

Step Two:

In the project APPDELEGATE.M-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (NSDictionary *) Launchoptions {} method in the initialization of Baidu Push, code as follows bold font;

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {

Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];

Self.window.backgroundColor = [Uicolor Whitecolor];

Baidu Push Send

[Bpush registerchannel:launchoptions apikey:@ "Here is the Baidu push platform gives you the application of ApiKey" Pushmode:bpushmodeproduction withfirstaction : nil Withsecondaction:nil Withcategory:nil Isdebug:yes];

Initialize Baidu push

Nsdictionary *userinfo = [Launchoptions Objectforkey:uiapplicationlaunchoptionsremotenotificationkey];

[Bpush Handlenotification:userinfo];

if (userInfo) {

NSLog (@ "Start from message:%@", userInfo);

[Bpush Handlenotification:userinfo];

}

#if Target_iphone_simulator

Byte dt[32] = {0xc6, 0x1e, 0x5a, 0x13, 0x2d, 0x04, 0x83, 0x82, 0x12, 0x4c, 0x26, 0xCD, 0x0c, 0x16, 0xf6, 0x7c, 0x74, 0x78, 0xb3, 0x5f, 0x6b, 0x37, 0x0a, 0x42, 0x4f, 0xe7, 0x97, 0xdc, 0x9f, 0x3a, 0x54, 0x10};

[Self application:application didregisterforremotenotificationswithdevicetoken:[nsdata dataWithBytes:dt length:32] ];

#endif

Angle SD 0

[[UIApplication sharedapplication] setapplicationiconbadgenumber:0];

Self.maincontroller = [[Maintabbarviewcontroller alloc] init];

Self.window.rootViewController = Self.maincontroller;

[Self.window makekeyandvisible];

return YES;

}

Note : 1, the Code of Apikey need to change to their own application in the Baidu push registration platform to the Apikey; (This represents your project in the Baidu push platform on the unique logo)

2, the code Pushmode: The two states are development status and production status, developers can change according to their current status.

Step three: or the APPDELEGATE.M file in the following method to the resulting devicetoken passed to the SDK.

-(void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) devicetoken{

[Bpush Registerdevicetoken:devicetoken];

[Bpush bindchannelwithcompletehandler:^ (id result, nserror *error) {

Nsuserdefaults *user = [Nsuserdefaults standarduserdefaults];

NSString *channle_id = result[@ "channel_id"];

[User setobject:channle_id forkey:@ "channel_id"];

[User Synchronize];

}];

}

And then in the following two methods (the method name is very similar to the above with block, here the two methods written in the same thing is the same) in the implementation of the relevant jump can be (given the example is for reference only, here to jump to the corresponding jump with local notification) Note: The specific jump needs to be with the background developer rules, debugging together.

-(void) Application: (UIApplication *) application didreceiveremotenotification: (nsdictionary *) userInfo Fetchcompletionhandler: (void (^) (uibackgroundfetchresult)) Completionhandler

{

Type = userinfo[@ "Push_type"];

Application in the foreground or background open state, do not jump page, let users choose.

if (application.applicationstate = = Uiapplicationstateactive | | application.applicationstate = = Uiapplicationstatebackground) {

NSLog (@ "acitve or background");

Uialertview *alertview =[[uialertview alloc]initwithtitle:@ "received a message" message:userinfo[@ "description"] Delegate:self cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "OK", nil];

[Alertview show];

}

Else

Kill the state, jump directly to the jump page.

{

if ([Type isequaltostring:@ "comment"]) {

Nsnotification *noti =[nsnotification notificationwithname:@ "jumptocomment" Object:nil UserInfo:nil];

[[Nsnotificationcenter Defaultcenter] postnotification:noti];

}else if ([Type isequaltostring:@ "reward"]) {

Nsnotification *noti =[nsnotification notificationwithname:@ "Jumptoreward" Object:nil UserInfo:userInfo];

[[Nsnotificationcenter Defaultcenter] postnotification:noti];

}else if ([Type isequaltostring:@ "Follow"]) {

Nsnotification *noti =[nsnotification notificationwithname:@ "Jumptofollow" Object:nil UserInfo:userInfo];

[[Nsnotificationcenter Defaultcenter] postnotification:noti];

}else if ([Type isequaltostring:@ "System"]) {

Nsnotification *noti =[nsnotification notificationwithname:@ "Jumptosystem" Object:nil UserInfo:userInfo];

[[Nsnotificationcenter Defaultcenter] postnotification:noti];

}

}

Completionhandler (Uibackgroundfetchresultnewdata);

}

-(void) Application: (UIApplication *) application didreceiveremotenotification: (nsdictionary *) userInfo

{

[Bpush Handlenotification:userinfo];

[Application Setapplicationiconbadgenumber:5];

Type = userinfo[@ "Push_type"];

Application in the foreground or background open state, do not jump page, let users choose.

if (application.applicationstate = = Uiapplicationstateactive | | application.applicationstate = = Uiapplicationstatebackground) {

NSLog (@ "acitve or background");

Uialertview *alertview =[[uialertview alloc]initwithtitle:@ "received a message" message:userinfo[@ "description"] Delegate:self cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "OK", nil];

[Alertview show];

}

else//kill state, jump directly to the jump page.

{

if ([Type isequaltostring:@ "comment"]) {

Nsnotification *noti =[nsnotification notificationwithname:@ "jumptocomment" Object:nil UserInfo:nil];

[[Nsnotificationcenter Defaultcenter] postnotification:noti];

}else if ([Type isequaltostring:@ "reward"]) {

Nsnotification *noti =[nsnotification notificationwithname:@ "Jumptoreward" Object:nil UserInfo:userInfo];

[[Nsnotificationcenter Defaultcenter] postnotification:noti];

}else if ([Type isequaltostring:@ "Follow"]) {

Nsnotification *noti =[nsnotification notificationwithname:@ "Jumptofollow" Object:nil UserInfo:userInfo];

[[Nsnotificationcenter Defaultcenter] postnotification:noti];

}else if ([Type isequaltostring:@ "System"]) {

Nsnotification *noti =[nsnotification notificationwithname:@ "Jumptosystem" Object:nil UserInfo:userInfo];

[[Nsnotificationcenter Defaultcenter] postnotification:noti];

}

}

}

iOS development three steps to fix Baidu push send

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.