通知点击回调函数

3

我正在尝试编写适用于Unity3D的插件,以便在应用关闭时获取用户单击本地或推送通知时的回调。我尝试了Prime31 Etcetera插件,但它无法工作。因此,我决定编写自己的插件。我尝试了几种方法,但它们都无法工作。是否有人可以指导我如何做到这一点?

1个回答

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();
        }

    }
}

希望这有所帮助!

这是针对iOS的,Unity为iOS提供了通知服务,但不支持安卓。我该如何在安卓上实现它? - Waqas Anwar
哎呀糟糕,我没有注意标签...我从来没有为Android做过这个,抱歉。也许你可以试试这个插件,它是免费的!(https://github.com/Agasper/unity-android-notifications) - Whatever

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接