如何在ios中显示前台的推送通知?

时间:2015-07-10 11:17:14

标签: ios xcode6 apple-push-notifications uialertview

我的应用有推送通知,我可以在提示中显示推送通知消息吗?

注意:当用户点击通知时,它将重定向到应用程序页面并且通知消失,因此此处显示警报中的总通知消息?在iOS应用程序中是否可以?

2 个答案:

答案 0 :(得分:0)

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

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

    NSLog(@"字典是%@",userInfo);

    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@" Notification"                                                         message:stringNotify委托:self                                               cancelButtonTitle:@"确定"                                               otherButtonTitles:nil,nil];     [alertView show];

}

答案 1 :(得分:0)

我创建了一个自定义通知,可以帮助您在应用处于前台时显示通知。 请查看以下链接iOSForegroundNotification,然后按照以下步骤操作:

  1. bundle exec guardSNotificationView.h复制到项目中的文件中。
  2. 如果您使用的是swift,请将SNotificationView.m添加到#import "SNotificationView.h"文件中。
  3. 复制“Glass.mp3”文件以获取通知声音。
  4. 您已将appicon图像替换/添加到“image.png”。

  5. 您已在AppDelegate文件中添加以下行:

    Bridging-Header.h
  6. 为所有ViewController

    添加以下功能
    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   ")
                }
             }
        }
    
  7. 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) }) }

    中添加以下行
    viewDidLoad
  8. 希望这可能会有所帮助