Tejas Sutar Tejas Sutar - 2 months ago 13x
iOS Question

How to show an alertview on receiving push notifications when app is not launched?

I am new to iOS development. I want to show an alertview on receiving a push notification while app is inactive.

Apple says that push notification can be presented in form of alert message or they can badge the application icon.


I am using PushSharp to send push notifications. But notifications are seen in notification center only. What should I do to show it on alertview?

I know we can write a code to show alertview like

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"test title" message:@"test message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[alert show];


But this code is executed after I tap notification and app launches. I want to show
as soon as device receives a push notification.

One more question, does Apple support any way to execute a code on receiving push notification though app is not launched?

Any ideas?


Not Possible with the UIAlertView . You can go through the UILocalNotification to achieve your task .

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
    notifaication = [[UILocalNotification alloc] init];
    notifaication.timeZone = [NSTimeZone systemTimeZone];

    notifaication.alertBody = // Set the Body
    notifaication.alertAction =// @"Show me";
    backupAlarm.soundName = UILocalNotificationDefaultSoundName;

For More info over_Here