How To Use deviceToken in applicationDidBecomeActive - ios

Iam able to get and use device token fine here:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
// Prepare the Device Token for Registration (remove spaces and < >)
NSString *deviceToken = [[[[devToken description]
stringByReplacingOccurrencesOfString:#"<"withString:#""]
stringByReplacingOccurrencesOfString:#">" withString:#""]
stringByReplacingOccurrencesOfString: #" " withString: #""];
}
I would like to get/use the deviceToken within:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// How To GET/USE device Token Here?
}

You can store device token in NSUserDefaults , so when ever you want it you can easily get it,
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
token = [token stringByReplacingOccurrencesOfString:#" " withString:#""];
[[NSUserDefaults standardUserDefaults] setObject:token forKey:#"token"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Now you can get it anywhere anytime till your app is not deleted.
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSString *token=[[NSUserDefaults standardUserDefaults]objectForKey:#"token"];
}

you can add a property in your appDelegate,like this:
then you can set the property in - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken ,and get the property in - (void)applicationDidBecomeActive:(UIApplication *)application.

It's best practice to convert deviceToken into Base64 NSString, so you no need to parse description. You can store this value in private member of you AppDelegate (using category), or public property.

Related

didRegisterUserNotificationSettings Being Called Twice?

I've been having an issue with my iOS app receiving duplicated over ever push notification. After doing some digging around StackOverflow, I figured out the issue could be because didRegisterUserNotificationSettings was being called twice.
So, I set a breakpoint on didRegisterUserNotificationSettings, and indeed, it is being called twice every time the app is launched.
The problem is, I'm only calling it once! Can someone please help me here and tell me why the delegate method didRegisterUserNotificationSettings is being called twice in the use case below:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[OneSignal initWithLaunchOptions:launchOptions appId:#"xxxx"];
return true;
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *devToken = [[[[deviceToken description] stringByReplacingOccurrencesOfString:#"<"withString:#""] stringByReplacingOccurrencesOfString:#">" withString:#""] stringByReplacingOccurrencesOfString: #" " withString: #""];
[[NSUserDefaults standardUserDefaults]setObject:devToken forKey:kUserDeviceTokenKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//firing twice!
NSLog("check");
}
You're using 1.* version of the SDK. Make sure to update to the latest 2.0.9 version to solve this issue.

Push notification permission alert does not appear

In the following code, the device token is gotten.
But Alert View which confirms push notification permission does not appear.
Maybe for that reason, although sending push notification, it does not reach the device.
Do you have any idea why the Alert does not appear?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *deviceTokenString = [deviceToken.description stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:#" " withString:#""];
NSLog(#"DeviceToken: %#", deviceTokenString);
}
Deployment target is iOS8.0.
Push Notifications status in Provisioning Profile is "Enabled".

Get device token for iOS push notifications at certain point in app

I've tried Googling, and keep hitting the same sorts of answers, ie.
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:#"<>"]];
token = [token stringByReplacingOccurrencesOfString:#" " withString:#""];
NSLog(#"content---%#", token);
}
However, I'm struggling to see how I might call/invoke this method in my App Delegate at a given event.
In my app, the first screen they see is login/signup. So on success of signup, I'd like to grab the device token and send it to my server to save against that new user. How exactly can I invoke this method from within a View Controller (or wherever) at a given time/event of my choosing, (and not just when the app first boots up)?
Hi try to use NSUserDefaults to store this data.
[[NSUserDefaults standardUserDefaults] setValue:deviceTokenString forKey:#"deviceToken"];
And to get this data use:
NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
NSString *string = [data objectForKey:#"deviceToken"];

Getting the Device ID before running ViewController code in IOS

I have this peace of code to get device id :
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *devicetokenRN = [NSString stringWithFormat:#"%#", deviceToken];
devicetokenRN = [devicetokenRN stringByReplacingOccurrencesOfString:#" " withString:#""];
devicetokenRN = [devicetokenRN stringByReplacingOccurrencesOfString:#"<" withString:#""];
devicetokenRN = [devicetokenRN stringByReplacingOccurrencesOfString:#">" withString:#""];
// send device token for APNS without spaces or <>
DeviceID *DeviceIdentifier = [[DeviceID alloc ] init];
// sending it to the Device.h
[DeviceIdentifier setDeviceID:devicetokenRN];
}
I need to retrieve this DeviceID as the first thing on my ViewController. As it doesn't do this function before it runs the view controller, because if I use NSlog in the ViewController it is NULL as it hasn't run this first. I need to get some variable like this first before it runs the UITableView. Any ideas would be brilliant thanks.
In AppDelegate, you can save the deviceToken value in NSUserDefaults like
put these line in didRegisterForRemoteNotificationsWithDeviceToken method in appdelegate.m like this
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[[NSUserDefaults standardUserDefaults] setObject:deviceToken forKey:#"DeviceToken"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
also put this line of code in your view Controller.m and In ViewDidLoad method
and Retrieve that value.
[[NSUserDefaults standardUserDefaults] objectForKey:#"DeviceToken"];
You can get and store your deviceId in NSUserDefaults
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSUUID *identifierForVendor = [[UIDevice currentDevice] identifierForVendor];
NSString *deviceId = [identifierForVendor UUIDString];
NSUserDefaults *userRegistrarionDetails = [NSUserDefaults standardUserDefaults];
[userRegistrarionDetails setObject:deviceId forKey:#"deviceID"];
[userRegistrarionDetails synchronize];
}
And you can get anywhere in your whole project by
NSUserDefaults *userRegistrarionDetails = [NSUserDefaults standardUserDefaults];
NSString * deviceId = [userRegistrarionDetails objectForKey:#"deviceID"];
NSLog(#"%#", deviceId);

Why my iPod touch 4 cannot register APNS?

I am trying to test my app on iPod touch 4, however I need APNS support.
I found out that neither delegate callbacks for APNS is called.
I've searched through the Technical Note, and it doesn't help (for the same code works for my iPad2, that the device token is returned).
Any idea on this?
I am using iPod touch 4 with iOS 5.1.1
Edit:
Ah, more information provided here.
I have create a provisioning profile before as well as all those certificates. That's why I can work that out with iPad.
Then today, I just added my iPod touch 4 as the development device and downloaded the provisioning profile again. Will this act not registering my iPod touch 4 to be able for handling APNS job????
Edit 2:
As requested:
Actually I just do whatever Apple requested me to do,
that I included
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge)];
//Other codes...
}
Then, the two delegates callback methods
#pragma mark - For Apple Push Notification Service
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *str = [NSString
stringWithFormat:#"%#",deviceToken];
NSString *devToken = [[[[str description]
stringByReplacingOccurrencesOfString:#"<"withString:#""]
stringByReplacingOccurrencesOfString:#">" withString:#""]
stringByReplacingOccurrencesOfString: #" " withString: #""];
NSLog(#"%#", devToken);
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults)
{
[standardUserDefaults setObject:devToken forKey:#"device_token"];
[standardUserDefaults synchronize];
}
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(#"application:didFailToRegisterForRemoteNotificationsWithError: %#", error);
}
Actually, I just copy and paste what Apple provided me.
However the case is just as mentioned above, either method is called for iPad2 with iOS 5.0.1, iPhone with iOS 4.3 but not for my iPod Touch 4 with iOS5.1.1.

Resources