【问题标题】:how to show push notification in foreground in ios?如何在ios的前台显示推送通知?
【发布时间】:2015-09-23 12:55:42
【问题描述】:

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

注意:当用户单击通知时,它将重定向到应用程序页面并且通知消失,所以这里在警报中显示总通知消息?在iOS应用程序中可以吗?

【问题讨论】:

标签: ios xcode6 apple-push-notifications uialertview


【解决方案1】:
  • (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" 消息:stringNotify 委托:self 取消按钮标题:@“确定” 其他按钮标题:无,无]; [alertView 显示];

}

【讨论】:

    【解决方案2】:

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

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

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

      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. 为所有的 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. 在您的 viewDidLoad 中添加以下行

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

    希望这可能会有所帮助

    【讨论】:

      猜你喜欢
      • 2019-06-24
      • 2019-06-26
      • 1970-01-01
      • 2019-02-07
      • 2015-08-25
      • 2020-06-28
      • 2016-01-23
      • 2018-10-24
      • 1970-01-01
      相关资源
      最近更新 更多