Just wanted a confirmation. Does the app ask for user permission if i have used silent push notification service like it use to ask when push notification was used. Any help would be appreciated
No. It's silent, meaning it works in the background, without user confirmation at the time of the notification.
At app install it asks for confirmation like a normal push notification.
For silent push you have to set JSON like this (php file),
$body['aps'] = array(
'sound' => '',
'content-available' => 1
);
don't send alert and badge.
In xcode file select target -> capabilities and enable background modes and tick remote notification
in appdelegate.m file just use this message to receive silent push
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
//Success this method will automatically call when device receives push notification
handler(UIBackgroundFetchResultNewData);
}
// eg code to register for apps
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
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];
}
return YES;
}
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
//handle the actions
if ([identifier isEqualToString:#"declineAction"]){
}
else if ([identifier isEqualToString:#"answerAction"]){
}
}
#endif
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(#"My token is: %#", deviceToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(#"Failed to get token, error: %#", error);
}
Related
I am trying to implement push notifications in my application. I am using APNs Authentication Key. Every guide I found so far has either deprecated method or it's in Swift. I am interested in a guide like this one but in Objective C.
What I have done so far, inside AppDelegate.m :
#import Firebase;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[FIRApp configure];
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self application:application didReceiveRemoteNotification:userInfo];
}
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
return YES;
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Perform different operations depending on the scenario
if (application.applicationState == UIApplicationStateInactive) {
// Opening the app
NSLog(#"Firebase opening app");
}
else if (application.applicationState == UIApplicationStateBackground) {
// Coming from background
NSLog(#"Firebase background");
}
else {
// Active
NSLog(#"Firebase active");
}
}
Unfortunately when I send a message from firebase I get nothing, no output in xcode Console and no notification.
I would appreciate it if you can point me to the right direction.
You do not implement the firebase apn token neither you connect to the database. Why not just implement it like here firebase example.
Did you even setup the plist and the POD libs? In the code example above there is only deprecated code for the iOS part of version 8/9 - means u can ignore these code lines.
Please use this method:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//Do something
}
instead of this:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//Do something
}
Update:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[FIRApp configure];
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)])
{
#ifdef __IPHONE_8_0
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert
| UIUserNotificationTypeBadge
| UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:settings];
#endif
}
else
{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
return YES;
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];
}
In my app, I have the following process for APNs:
Register the device in didFinishLaunchingWithOptions;
Receive the token with didRegisterForRemoteNotificationsWithDeviceToken;
Send it to my Notification server;
My server sends the notification;
PROBLEM: My device don't receive it (didReceiveRemoteNotification never called on Debug);
Already checked the App Id on the server and certificate.
Anyone can point what am I doing wrong?
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Checking if app is running iOS 8
if ([application respondsToSelector:#selector(registerForRemoteNotifications)]) {
// Register device for iOS8
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge |
UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:notificationSettings];
[application registerForRemoteNotifications];
} else {
// Register device for iOS7
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge];
}
//...
return YES;
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(#"Registration successful, bundle identifier: %#, device token: %#",
[NSBundle.mainBundle bundleIdentifier], deviceToken);
// Receive the token and send it to my Server
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(#"Error in registration. Error: %#", err);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Handle notification
}
Lefteris and Rajat are right, my problem was the wrong certificate for the enviroment.
I know this question already asked previously and also have some solution for it,i tried all the solution available ,but nothing is working for me. Code is working perfectly on my side but when i submit the App store they reject it (till now i got 3 rejections from App store).
I try the the code from the following link:-
why didRegisterForRemoteNotificationsWithDeviceToken is not called
Get Device Token in iOS 8
https://developer.apple.com/library/ios/technotes/tn2265/_index.html
I also check provisional profile and certificates they all are fine.
Anyone please help
Push notification registration process has been changed in iOS 8, Make sure you have added this
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
I have used this way. I didn't get any issue from app store.
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
}
#endif
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)notification completionHandler:(void(^)())completionHandler
{
NSLog(#"Received push notification: %#, identifier: %#", notification, identifier); // iOS 8 s
completionHandler();
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
{
NSString *newToken = [devToken description];
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:#" " withString:#""];
NSLog(#"NotificationToken: %#",newToken);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(#"Error while registering for notifications: %#", error);
}
I use UILocalNotification in one app and in iOS 7 or earlier is working fine, but in iOS 8 didReceiveLocalNotification is not being called at all.
After some research I found out that in iOS 8 we have to use handleActionWithIdentifier instead, and so I tried it but also this void is not being called when LocalNotification run.
Here is my code:
I've got this code in didFinishLaunchingWithOptions and it works ok, first time I launched the app I've been asked permission to give notifications etc.
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
Then I got:
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification
completionHandler:(void(^)())completionHandler
{
NSLog(#"handleAction");
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(#"didReceive");
}
When the localNotification run and I slide my finger on the message that appears on the screen nothing happens... logs are not called at all...
I also noticed that if the app is active and running, when the localNotification run, it get called the method didReceiveLocalNotification
Anyone know what am I doing wrong? Thanks for any help
add this to your didFinishLaunchingWithOptions:
if ([[UIApplication sharedApplication]respondsToSelector:#selector(isRegisteredForRemoteNotifications)])
{
// iOS 8 Notifications
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
// iOS < 8 Notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
You forgot to add this line:
[[UIApplication sharedApplication] registerForRemoteNotifications];
According to the Apple documentation of application:handleActionWithIdentifier:forLocalNotification:completionHandler::
Your implementation of this method should perform the action associated with the specified identifier and execute the block in the completionHandler parameter as soon as you are done. Failure to execute the completion handler block at the end of your implementation will cause your app to be terminated.
Your implementation above is missing the completion handler block.
To make it work, modify your application:handleActionWithIdentifier:forLocalNotification:completionHandler: to be as follows:
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
{
NSLog(#"handleAction");
if (completionHandler) {
completionHandler();
}
}
This method calle when the user taps a custom action in the alert for a remote or local notification’s.
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandle
{
//some action
}
When you slide finger on screen called this method
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
}
While I see you are handling the "forRemoteNotification:" case,
I don't see this being defined in your original block of code:
application:handleActionWithIdentifier:forLocalNotification:completionHandler:
I am creating an application which support push notification
I am following all the steps.
It give error on simulator
Failed to get token, error: Error Domain=NSCocoaErrorDomain Code=3010 "remote notifications are not supported in the simulator" UserInfo=0x5813d20 {NSLocalizedDescription=remote notifications are not supported in the simulator}
But on device it not calling the delegates methods
didFailToRegisterForRemoteNotificationsWithError
didRegisterForRemoteNotificationsWithDeviceToken
My Code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
NSLog(#"application didFinishLaunchingWithOptions");
// Override point for customization after application launch.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSLog(#"My Token is %#",deviceToken);
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(#"Failed to get token, error: %#", error);
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(#"Received Notification %#", userInfo);
}
Push notification won't work in iPhone's Simulator.
Try to check with device.
Solved for me:
In Device, go to:
`Settings->Notifications->YourApp->Enable_Notifications`
As said by #kalyan Push Notification will NOT work in Simulator
But if you want to get ride of the warning here is the code to work
with both Simulator and device (Check for TARGET_IPHONE_SIMULATOR )
and iOS7 and iOS8 ( Check for registerUserNotificationSettings )
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#if !TARGET_IPHONE_SIMULATOR
// New User Notification Settings for iOS8 support
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else { // for iOS 7
UIRemoteNotificationType remoteNotificationType = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
remoteNotificationType];
}
#endif
}