0

my app had a push notifications , can i show the push notification message in alert ?

NOTE:When user click on the notification it will redirect to the application page and notification disappears so here show total notification message in an alert? Is it possible in iOS application?

Kranthi
  • 79
  • 1
  • 8

2 Answers2

0
  • (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    NSString *stringNotify = [NSString stringWithFormat:@"%@",[[[userInfo valueForKey:@"aps"] valueForKey:@"alert"] valueForKey:@"body"]];

    NSLog(@"the dictionary is %@",userInfo);

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Notification" message:stringNotify delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alertView show];

}

Kranthi
  • 79
  • 1
  • 8
0

I have created a custom Notification, which will help you to show notification when your app is in foreground. Check the following link iOSForegroundNotification and follow the steps given below:

  1. Copy SNotificationView.h and SNotificationView.m in files in your project.
  2. If you are using swift then add #import "SNotificationView.h" to your Bridging-Header.h file.
  3. Copy "Glass.mp3" file for the notification sound.
  4. You have replace/add you appicon image to "image.png".

  5. You have add the following lines in your AppDelegate file:

    let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
      if let aps = userInfo["aps"] as? NSDictionary {
        if let alert = aps["alert"] as? NSDictionary {
    
    
            if let message = alert["message"] as? NSString {
    
                prefs.setObject(message, forKey: "notification")
                prefs.synchronize()
                print("Message: \( message)\n")
            }
        } else if let alert = aps["alert"] as? NSString {
    
            // Push notification message is added in NSUserDefault
            prefs.setObject(alert, forKey: "notification")
            prefs.synchronize()
    
          if (application.applicationState == UIApplicationState.Active ) {
    
             if settings?.types & UIUserNotificationType.Alert != nil {
                    // .Alert is one of the valid options
                   // If you want to add the badge add the line below
    
                     UIApplication.sharedApplication().applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
    
                    //Call the custom Notification
    
                    NSNotificationCenter.defaultCenter().postNotificationName("remotenotification", object: nil)
    
                }
                else
                {
                    // This part will be called if you app notification is set to "NONE"
                    print("No alert   ")
                }
             }
        }
    
  6. Add the following function for all your ViewController

    func callNotification()
    {
        let prefs = NSUserDefaults.standardUserDefaults()
        let alert = prefs.stringForKey("notification")!
    
        SNotificationView.showNotificationViewWithImage(UIImage(named:"image.png"), title: "XcodeProject", message: alert, isAutoHide: true, onTouch: {        
        SNotificationView.hideNotificationViewOnComplete(nil)
    
        })
    }
    
  7. Add the following line in your viewDidLoad

       NSNotificationCenter.defaultCenter().addObserver(self, selector: "callNotification", name: "remotenotification", object: nil)
    

Hope this might be helpful

Karlos
  • 1,653
  • 18
  • 36