【问题标题】:What events are triggered when a user clicks on a notification in notification center in ios/xamarin当用户点击 ios/xamarin 通知中心的通知时会触发哪些事件
【发布时间】:2015-02-01 14:55:02
【问题描述】:

我正在使用 ios/xamarin 构建移动应用程序,我想确定当应用程序在后台运行或未激活时,当用户单击 ios/xamarin 通知中心中的通知时将触发哪些事件。

我检查了ReceivedRemoteNotification 事件是否在我点击通知中心的通知时触发,无论应用是否处于活动状态。

【问题讨论】:

    标签: c# ios xamarin apple-push-notifications


    【解决方案1】:

    当应用程序处于活动状态时:

    如果您的应用程序处于活动状态,则默认调用以下方法来接收通知:

    DidReceiveRemoteNotification

    如果您想默认调用 ReceivedRemoteNotification,那么您必须添加键 UIBackgroundModes 和值 remote-notificationinfo.plist 文件中

    当应用程序在后台时

    iOS 会自行显示收到的通知。目前在这种情况下无法调用任何方法。因此,当用户再次点击收到的通知时,将调用 DidReceiveRemoteNotification 方法,您将在其中收到有关详细信息的通知。

    当应用程序处于非活动状态时:

    iOS 会自行显示收到的通知,在这种情况下再次无法调用任何方法。现在,当用户点击收到的通知时,将调用 FinishedLaunching 方法并使用以下代码,您可以获取本地和远程通知的数据:

    if (options != null)
                {
                    // check for a local notification
                    if (options.ContainsKey(UIApplication.LaunchOptionsLocalNotificationKey))
                    {
                        UILocalNotification localNotification = options[UIApplication.LaunchOptionsLocalNotificationKey] as UILocalNotification;
                        if (localNotification != null)
                        {
                            //--your code
                        }
                    }
    
                    // check for a remote notification
                    if (options.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey))
                    {
                        NSDictionary remoteNotification = options[UIApplication.LaunchOptionsRemoteNotificationKey] as NSDictionary;
                        if (remoteNotification != null)
                        {
                            //--your code
                        }
                    }
                }
    

    【讨论】:

    • 很好解释。你能详细说明一下默认调用 ReceivedRemoteNotification 吗?
    【解决方案2】:

    检查 Xamarin documentation。根据使用情况,另一个事件是ReceivedLocalNotification。对于远程通知,那里还描述了注册工作流程。

    【讨论】:

    • 谢谢,我试过了,但是当我收到远程通知时没有触发 ReceivedLocalNotification,我正在检查文档,但到目前为止还是一样。
    【解决方案3】:

    但是如果您或其他人仍在寻找答案,那么老问题..

    此操作没有调用任何事件,但您可以通过使用 FinishedLaunching 方法附带的“选项”变量单击本地和/或远程通知来确定您的应用程序是否已启动。

    如果为null,您的应用程序已正常启动,如果不为null,则可能是通过单击通知中心的通知启动的。

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        /* 
        // Your init code, for ex.
        global::Xamarin.Forms.Forms.Init();
        LoadApplication(new App(string.Empty));
        */                                                      
    
        if (options != null)
            if (options.Keys != null)
                if (options.Keys.Count() != 0)
                {
                    UILocalNotification localnotif = null;
    
                    if (options.ContainsKey(UIApplication.LaunchOptionsLocalNotificationKey))
                        localnotif = options.ObjectForKey(UIApplication.LaunchOptionsLocalNotificationKey) as UILocalNotification;
    
                    if (localnotif != null)
                    {
    
                        UIAlertView alert = new UIAlertView()
                        {
                            Title = "LocalNotification",
                            Message = string.Format("Content: {0}, {1}", localnotif.AlertTitle, localnotif.AlertBody)
                        };
                        alert.AddButton("OK");
                        alert.Show();                                        
                    } 
                }
    
        return base.FinishedLaunching(app, options);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      相关资源
      最近更新 更多