2016-12-09 22 views
2

私は、ユーザーがローカルまたはプッシュ通知をクリックしたときにコールバックを取得するために、Unity3Dのプラグインを作成しようとしています。 Prime31 Etcetera Pluginを試しましたが、うまくいきません。だから自分のプラグインを書くことにしました。私はいくつかの方法を試したが、うまくいかなかった。誰も私にそれをする方法を導くことができますか?通知がクリックされましたコールバック

答えて

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

    } 
} 

これは役に立ちます。

+0

これはiOS向けです。UnityはiOS向けの通知サービスを提供しますが、Androidは提供しません。どのように私はアンドロイドでそれを達成することができますか? –

+0

おっと、私の悪い、タグに注意を払わなかった..私はアンドロイドのためにそれをしなかった、申し訳ありません。おそらくあなたはこのプラグインを試すことができます、それは無料です! (https://github.com/Agasper/unity-android-notifications) – Whatever

関連する問題