Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell if my iPhone app is running when a Push Notification is received?

I am sending Push Notifications to my iPhone app, and I'd like a different set of instructions to execute depending on whether the app is already launched or not. I'm new to iPhone development, and while I suspect UIApplication or my project's AppDelegate class has the solution, I haven't found a good answer. Is there an easy way to check for this?

like image 604
thebossman Avatar asked Jan 04 '10 08:01

thebossman


People also ask

How do I know if push notifications are on?

Turn on notifications for Android devicesTap More on the bottom navigation bar and select Settings. Tap Turn on notifications. Tap Notifications. Tap Show notifications.

Does an app need to be open for push notifications?

A mobile app uses “push notification” to send users a message that notifies them about something important, without the need to open the app. The user does not need to do anything, as the app itself decides to push an alert to them, which can be in the form of a text message or an image.

Do push notifications work when app is closed iOS?

Apple does not offer a way to handle a notification that arrives when your app is closed (i.e. when the user has fully quit the application or the OS had decided to kill it while it is in the background). If this happens, the only way to handle the notification is to wait until it is opened by the user.

How do I know if push notifications are on iPhone?

Find your notifications in Notification CenterOn the Lock Screen: Swipe up from the middle of the screen. On other screens: Swipe down from the top center. Then you can scroll up to see older notifications, if there are any.


1 Answers

Here's the more appropriate way of handling active/inactive state of the app.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {            // check for the app state         UIApplicationState state = [application applicationState];          if (state == UIApplicationStateActive) {             //the app is in the foreground, so here you do your stuff since the OS does not do it for you             //navigate the "aps" dictionary looking for "loc-args" and "loc-key", for example, or your personal payload)         }      application.applicationIconBadgeNumber = 0; } 

didReceiveRemoteNotification: is called when the app is running, yes, but when it is suspended, the iOS takes care of putting up the badge, etc. If the app is in the foreground, the OS does nothing, and just calls your didReceiveRemoteNotification:.

like image 143
Marco Papa Avatar answered Oct 06 '22 23:10

Marco Papa