【问题标题】:UnityEngine.iOS.NotificationServices.CancelLocalNotification does not remove notificationsUnityEngine.iOS.NotificationServices.CancelLocalNotification 不删除通知
【发布时间】:2018-01-12 11:48:58
【问题描述】:

我目前正在为我的 iOS 版 Unity3D 游戏构建一个类来处理通知。但是当我使用UnityEngine.iOS.NotificationServices.CancelLocalNotification (nt); 时,预定的通知没有任何反应。

有人可以帮助指点如何使这项工作吗?

作为替代方案,我可以使用UnityEngine.iOS.NotificationServices.CancelAllLocalNotifications,但这会不那么优雅。

我在 iphone 6S 上使用 Unity 2017.2.0f3 和 iOS 11.2.1。

public void UpdateMorningNotifications()
{

    // Get all current scheduled morning notifications in a list. if they are type morning. erase them.
    UnityEngine.iOS.LocalNotification[] scheduledNotifications = UnityEngine.iOS.NotificationServices.scheduledLocalNotifications;

    Debug.Log ("NotificationScheduler/UpdateMorningNotifications - number of scheduledNotifications = " + scheduledNotifications.Length);

    foreach (UnityEngine.iOS.LocalNotification nt in scheduledNotifications) 
    {

        Debug.Log ("NotificationScheduler/UpdateMorningNotifications - clearing notification, type is " + nt.userInfo["type"] );
        if (nt.userInfo ["type"] == "morning") 
        {
            Debug.Log ("Its a morning nt, lets get rid of it!");
            UnityEngine.iOS.NotificationServices.CancelLocalNotification (nt);
            Debug.Log ("NotificationScheduler/UpdateMorningNotifications - clearing notification: " + nt.alertBody);
        }

    }


    // building, and adding notifications to notificationservices.
    DateTime fireDate = DateTime.Today;

    int summedInterval = 0;
    // fill up the morning timings list.
    foreach (int interval in MorningNotificationDayInterVals) 
    {   
        // adding new interval to firedate
        summedInterval = summedInterval + interval;
        fireDate = fireDate.AddDays (summedInterval);


        // if weekend fire at 10AM else 8 AM
        if (fireDate.DayOfWeek.ToString() == "Saturday" || fireDate.DayOfWeek.ToString() == "Sunday") {
            fireDate.AddHours (10);
        } else {
            fireDate.AddHours(8);
        }

        //generate notification with random title and the timing and add it to the batch.
        int morningTitleIndex = UnityEngine.Random.Range(0,morningTitles.Length);

        // generating a new notification.
        notification = GenerateNotification (morningTitles[morningTitleIndex], fireDate, "morning");

        // adding the notification to the batch.
        notificationBatch.Add (notification);
        Debug.Log("NotificationScheduler/UpdateMorningNotifications: Length of notificationBatch = " + notificationBatch.Count);
    }

    // Adding notifications to notificationsservices
    AddNotificationBatchToNotificationServices (notificationBatch);


}

【问题讨论】:

    标签: c# ios unity3d notificationservices


    【解决方案1】:

    解决了。有一个错误。 if (nt.userInfo ["type"] == "morning") 应该是 if (nt.userInfo ["type"].ToString() == "morning")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多