Get PushNotification when app is opened in objective c [duplicate] - ios

This question already has answers here:
iOS push notification received when app is running in foreground
(3 answers)
Closed 6 years ago.
How to get Push Notifications when app is opened. Get Push Notifications only when app is close or in background.

When the app is background or foreground
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler
will be invoked when app is not in memory ,user interaction is needed on the push notification which opens the app and need to handle it on
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Your question is not very clear but if you want to show notifications in notification window even when your app is active then you need to handle the same in
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)pushMessage
and post a new local notification with the same payload. which you can then handle in
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif

Generally If the application is running in the foreground, iOS won't
show a notification banner/alert.That's by design.When app is in forground if you want to show push notification you can use UserNotification Framework for iOS 10 and it shows or displaysnotification as banner.Also you can use UILocalNotification to achieve this.Mainly we use didReceiveRemoteNotification method for achieving this.Now we have UIUserNotification methods from iOS 10.
When application is foreground and background state getting push notification
Showing Push Notification in foreground state iOS 10

Related

Push not executing while app is closed IOS

I use push notifications but while my app is closed,and push received it is not executed(i perceived by putting break point didreceived method),only appears on screen.What can i do to detect push received while app is closed?
I use also background fetch and remote notifications in target->Capabilities
MY Codes :
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^) (UIBackgroundFetchResult))completionHandler {
1) You have to set "content-available" to 1 in the notification body.
2) And have the right background modes: Background Modes
Also: Your users can turn this off by disabling Background App Refresh in the iPhone Settings. You can check this in code:
[UIApplication sharedApplication].backgroundRefreshStatus!=UIBackgroundRefreshStatusAvailable

Push to wake a backgrounded iOS 8 app does not get any data

There's a new feature in iOS 8: using a push notification to wake an app, which also lets the app refresh data in the background. How do I use this feature?
The push notification arrives with a value of 1 for the key 'content-available'. However, no data is received if the app is not already running in background. Why not?
There are couple of things here,
1.U need to check Remote Notification on under Background mode of the target in Capabilities section.
2.U need to implement below method,
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

Getting push notification payload when user opens app manually after push has been received in the background

I am using iOS7 and I am trying to determine if I can get the JSON payload in the following situation.
I have background mode "remote-notifications" enabled
The push notification is received while the app is terminated
The app is launched manually from the icon not from the notification center
When I launch the app from the icon itself after the notification has been received I do not get the push in the launch options from
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
and the following method does not get called either when app is manually launched from the icon
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
I finally figured out how you can get this!
As of iOS 7 you can get it!
Basically, you need to configure your application for background remote notifications.
So, in your info.plist file:
For required backgrounds - set it to app downloads content from push notifications.
In the AppDelegate.m file, you need to implement this method:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
See this for how to implement that: didReceiveRemoteNotification: fetchCompletionHandler: open from icon vs push notification
For your push notifications, you must have 'content-available': 1, as part of the push notification. This is what tells the application that there is new content before displaying the alert.
See this page for more information on background remote notifications: http://developer.xamarin.com/guides/cross-platform/application_fundamentals/backgrounding/part_3_ios_backgrounding_techniques/updating_an_application_in_the_background/

iOS Silent push notification is not working if I killed app manually [duplicate]

This question already has answers here:
Will iOS launch my app into the background if it was force-quit by the user?
(7 answers)
Closed 9 years ago.
I have implemented silent push notification. Its not working if I killed app manually.
After getting silent push notification, I have called one function which will send data to server.
Here is my code for Silent Push notification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
NSLog(#"didReceiveRemoteNotification fetchCompletionHandler...........");
[MyviewController SendDataFunction];
handler(UIBackgroundFetchResultNewData);
}
Please guide me.
Update:
-There is no way to launch app via silent notification or via background fetch if user has killed app manually in app switcher. To get notification user must start the app again. And still if anybody needs to activate app then PushKit + VOIP should be enabled for that app like whatsapp, refer the following link
https://zeropush.com/guide/guide-to-pushkit-and-voip
didReceiveRemoteNotification is only called app when running and app in background state, it not called app not running state, that time you get notifications like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...............
...............
UILocalNotification *localNotif =[launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSLog(#"load notifiation *******");
// Perform your actions
}
return YES;
}

Apple Push notification not deliver on ios 7 when application remove from background

I have tried silent Apple push notification in ios 7 using following code. The Push notification received when application is in foreground or in background. Then I remove application from background by swiping the application from background apps. After that If I send a Push notification from my server, it sent to APN properly, but not delivered to iPhone.So Apple push notification in ios 7 delivers silent push notification only application running in backgroud? If user remove application from background will it receive notification or not?
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSString *receivedMessage = [[userInfo objectForKey:#"acme1"] objectForKey:#"mydata"];
NSLog(#"fetchCompletionHandler receivedMessage -> %#",receivedMessage);
completionHandler(UIBackgroundFetchResultNewData);
}
didReceiveRemoteNotification not called when app will not run,it only called when app will open and foreground.
you will get list of notifications in UILocalNotification,if app not run and it receive notifications like this :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
.........
.........
.........
UILocalNotification *localNotif =[launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSLog(#"****** notifiation ******");
}
return YES;
}
App will be able to receive the notification even when app is closed i.e. not in background mode.

Resources