通知点击了回拨

时间:2016-12-09 11:19:48

标签: android unity3d push-notification android-notifications

我正在尝试为Unity3D编写一个插件,以便在应用关闭时用户点击本地或推送通知时获取回调。我为此试过了Prime31 Etcetera插件,但它没有用。所以我决定编写自己的插件。我尝试了几种方法,但它没有用。谁能指导我怎么做?

1 个答案:

答案 0 :(得分:1)

我几天前不得不这样做。这是我使用的课程:

using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.iOS;
using System.Collections;

public class Notifications : MonoBehaviour
{
    private void Awake()
    {
        // If app launched for the first time, set up a notification
        if (!PlayerPrefs.HasKey("firstLaunch"))
        {
            UnityEngine.iOS.NotificationServices.ClearLocalNotifications();
            UnityEngine.iOS.NotificationServices.CancelAllLocalNotifications();

            PlayerPrefs.SetInt("firstLaunch", 0);

            UnityEngine.iOS.NotificationServices.RegisterForNotifications(NotificationType.Alert | NotificationType.Badge | NotificationType.Sound);

            var notification = new UnityEngine.iOS.LocalNotification();
            notification.alertAction = "Title of the notification";
            notification.alertBody = "Body of the notification";
            // I set this to -1 because I didn't want to have the badge on the app icon
            notification.applicationIconBadgeNumber = -1;

            var currentDate = DateTime.Now;
            var targetDate = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, 9, 15, 00, DateTimeKind.Local);

            targetDate = (currentDate < targetDate) ? targetDate : targetDate.AddDays(1);

            // Set the date and time the notification will fire
            notification.fireDate = targetDate;
            // In my case in wanted to repeat it everyday at 9:15 AM
            notification.repeatInterval = UnityEngine.iOS.CalendarUnit.Day;

            UnityEngine.iOS.NotificationServices.ScheduleLocalNotification(notification);
        }
    }

    private void OnApplicationPause(bool state)
    {
        // If app returns to foreground
        if (!state)
        {
            // If this count > 0, then a notification has been clicked
            if (UnityEngine.iOS.NotificationServices.localNotificationCount > 0)
            {
                // Do stuff
            }

            UnityEngine.iOS.NotificationServices.ClearLocalNotifications();
        }

    }
}

希望这有帮助!