ios8 - Get Device Token in iOS 8 -
i need device token implement push notification in app.
how can device token since didregisterforremotenotificationswithdevicetoken
method not working on ios 8. tried code in app delegate not giving me device token.
[[uiapplication sharedapplication] registerusernotificationsettings:[uiusernotificationsettings settingsfortypes:(uiusernotificationtypesound | uiusernotificationtypealert | uiusernotificationtypebadge) categories:nil]]; [[uiapplication sharedapplication] registerforremotenotifications];
read code in uiapplication.h.
you know how that.
first:
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
add code this
#ifdef __iphone_8_0 //right, point uiusernotificationsettings *settings = [uiusernotificationsettings settingsfortypes:(uiremotenotificationtypebadge |uiremotenotificationtypesound |uiremotenotificationtypealert) categories:nil]; [[uiapplication sharedapplication] registerusernotificationsettings:settings]; #else //register receive notifications uiremotenotificationtype mytypes = uiremotenotificationtypebadge | uiremotenotificationtypealert | uiremotenotificationtypesound; [[uiapplication sharedapplication] registerforremotenotificationtypes:mytypes]; #endif
if not using both xcode 5 , xcode 6 ,try code
if ([application respondstoselector:@selector(registerusernotificationsettings:)]) { uiusernotificationsettings *settings = [uiusernotificationsettings settingsfortypes:(uiremotenotificationtypebadge |uiremotenotificationtypesound |uiremotenotificationtypealert) categories:nil]; [application registerusernotificationsettings:settings]; } else { uiremotenotificationtype mytypes = uiremotenotificationtypebadge | uiremotenotificationtypealert | uiremotenotificationtypesound; [application registerforremotenotificationtypes:mytypes]; }
(thanks @zeiteisen @dmur 's remind)
second:
add function
#ifdef __iphone_8_0 - (void)application:(uiapplication *)application didregisterusernotificationsettings:(uiusernotificationsettings *)notificationsettings { //register receive notifications [application registerforremotenotifications]; } - (void)application:(uiapplication *)application handleactionwithidentifier:(nsstring *)identifier forremotenotification:(nsdictionary *)userinfo completionhandler:(void(^)())completionhandler { //handle actions if ([identifier isequaltostring:@"declineaction"]){ } else if ([identifier isequaltostring:@"answeraction"]){ } } #endif
and can devicetoken in
- (void)application:(uiapplication *)application didregisterforremotenotificationswithdevicetoken:(nsdata *)devicetoken
if still not work , use function , nslog error
-(void)application:(uiapplication *)application didfailtoregisterforremotenotificationswitherror:(nserror *)error
Comments
Post a Comment