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".
Related
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.
Currently one of my php developer provided me push notification API for iOS devices
The problem is : If i run that api with respective parameter in any Browser(Chrome/Safari/Firefox and etc..) i am getting notification on foreground of iOS device. But not in iOS app(Xcode) itself
In my app i used code like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Register for Push Notitications, if running on iOS 8
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}
#pragma mark
#pragma mark -- Push Notification Delegate Methods
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings: (UIUserNotificationSettings *)notificationSettings{
//register to receive notifications
[application registerForRemoteNotifications];
}
-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
// Prepare the Device Token for Registration (remove spaces and < >)
devToken = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:#"<"withString:#""]
stringByReplacingOccurrencesOfString:#">" withString:#""]
stringByReplacingOccurrencesOfString: #" " withString: #""];
NSLog(#"My token is: %#", devToken);
// My token is: cd2887c4093569b3eed142320f21a81e521e486cf5c40467390901d3a191398b
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:deviceToken forKey:#"deviceToken"];
}
-(void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{
NSLog(#"Failed to get token, error: %#", error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(#"%s..userInfo=%#",__FUNCTION__,userInfo);
}
I am getting response:(in didReceiveRemoteNotification)
{
aps = {
alert = "You're login successfully";
sound = default;
};
}
This message is not showing on Status bar(top of the screen). Is there any issue in iOS side (or) PHP side
If the issue is in iOS side--> How can i do this
Here is my Testing Push notification API:
https://shopgt.com/mobile1/iphone/register.php?device_type=2&email=sukhpal#anaad.net®Id=4d1d9067cc1382ecb8b0532831cce7fc8eb6fc388a6139060cd84712407a0ae5
Can you please help me out regarding this issue
You need to customize the view for showing Banner of Push Notification while the app in Foreground. You can use JCNotificationBannerPresenter. Follow the sample code using below link.
https://github.com/jcoleman/JCNotificationBannerPresenter
#import "JCNotificationCenter.h"
#import "JCNotificationBannerPresenterSmokeStyle.h"
- (void) application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)notification {
NSString* title = #"Push Notification";
NSDictionary* aps = [notification objectForKey:#"aps"];
NSString* alert = [aps objectForKey:#"alert"];
[JCNotificationCenter
enqueueNotificationWithTitle:title
message:alert
tapHandler:^{
NSLog(#"Received tap on notification banner!");
}];
}
Hope it Helps you..!
There is no issue, this is default behaviour.
The banner that appears at the top of the screen when you are on the Home screen does not appear when you are inside the app.
You need to get the app to do something with the notification yourself, inside didReceiveRemoteNotification. This could be showing an alert, adding a badge to the tab bar, etc. but banners only show when you are outside of the app.
Hi Im currently developing an app where i have push notifications activated. I use parse.com. I have got it working so far that i can send a notification and the device receives it and i also get a badge on the app. But when i open the nothing happens and the badge does not disappear. I've set it in my appdelegate.m so parse is handling the push notifications. Heres some code that im using:
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken {
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:newDeviceToken];
[currentInstallation saveInBackground];
}
and also:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
}
Thanks
Ok thaks but how to i show the content of the push notification in the alert view? Or is that not a problem do i just have to configure a alertview with no text?
There are two scenarios:
App closed
When the app is closed and the user taps on a notification it's like when taps on the app icon. The app starts from application:didFinishLaunchingWithOptions:. To know if the app is opened normal (tap icon) or with a notification, just check the dictionary launchOptions for the key UIApplicationLaunchOptionsRemoteNotificationKey.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary * pushDictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (pushDictionary)
{
//AlertView
}
}
App opened
When the app is opened there are two possible case
App in background UIApplicationStateInactive
App in foreground UIApplicationStateActive
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (application.applicationState == UIApplicationStateInactive)
{
//AlertView
}
else if (application.applicationState == UIApplicationStateActive)
{
// AlertView
}
}
pushDictionary and userInfo are exactly the same dictionary that represents the notification.
Edit 1
A push notification is a JSON file that iOS automatically convert into a NSDictionary. The standard configuration is:
{"aps":
{
"alert":"Text message",
"sound":"default",
"badge":"1" //the number shown on the app's icon
}
}
From this base you can extend the JSON and put inside your own content. For example
{"aps":
{
"alert":"Text message",
"sound":"default",
"badge":"1" //the number shown on the app's icon
},
"Name":"Fry"
}
Now you can retrive the name in this very simple way:
NSString * name = userInfo[#"Name"];
and show it in the alert.
Edit 2
To show the content of push in UIAlertView just do this:
UIAlertView *av = [[UIAlertView alloc] initWithTitle:name
message:name
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[av show];
If you receive push notification while your app isn't active (killed)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
will not be triggered. Instead of that you will have to use object with key UIApplicationLaunchOptionsRemoteNotificationKey from launchOptions dictionary inside
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
I am using notification in one of my app ,when app is active , notification is received , i process data and everything is fine , but I do not receive any notification when app is in background or killed.What is the issue can any one plz help me ?
Thank you!
Here is what I doing so far
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
tokenstring = [[NSString alloc] initWithFormat:#"%#",deviceToken];
tokenstring = [tokenstring stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
tokenstring = [[NSString alloc]initWithFormat:#"%#",[tokenstring stringByReplacingOccurrencesOfString:#" " withString:#""]];
NSLog(#"TokeinID:>> %#",tokenstring);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"didReceiveRemoteNotification: %#",userInfo);
AudioServicesPlaySystemSound(1002);
//Some code or logic
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(#"didFailToRegisterForRemoteNotificationsWithError: %#",err.description);
}
When you receive remote notifications, -application:didReceiveRemoteNotification: is only called when your app is in the foreground. If your app is in the background or terminated, then the OS may display an alert or play a sound (depending on the aps dictionary in the notification), but the delegate method is not called.
The remote notification received in the background will only passed to your application if it is launched with that notification's action button, and then you need to look at the launch options dictionary on -application:didFinishLaunchingWithOptions: to see the content of the notification.
If you're looking at new content fetching/remote notification background support with iOS7, check Will iOS launch my app into the background if it was force-quit by the user? and see if that helps, as there are very specific circumstances that those functions work in.
little code ..
When app is not running
(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
is called ..
where u need to check for push notification
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification) {
NSLog(#"app recieved notification from remote%#",notification);
[self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
}else{
}
i implemented the Remote Notifications in my application! if my App is in Background and a Push Message was send to my Device, i react with this method:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
...Do Stuff
}
This is working great when App is in Foreground or in Background State! But what if my App is not running at all?! CanĀ“t i react to Push Messages when the app is not running?I mean WhatsApp can do this, right?!
If user clicks on push notification from notification center you will have information in launchOptions with the push notification content and you can use below code to check if application was launched clicking push notification or it was there as well,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
NSLog(#"LaunchOptions->%#",launchOptions);
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self performNotificationAction:userInfo];
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
// NSLog(#"userInfo->%#",userInfo);
[self performNotificationAction:userInfo];
}
-(void)performNotificationAction:(NSDictionary*)userInfo{
//Do the stuf whatever you want.
//i.e. fetch the message or whatever extra information sent in push notification
}