Created
June 10, 2020 18:56
-
-
Save neon-izm/e09f5ab8fb18353af21a4ea660e1b0f3 to your computer and use it in GitHub Desktop.
UnityEditorからプッシュ通知を特定端末に送るサンプル com.unity.editorcoroutines@1.0 を前提にしています。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using System.Text; | |
using Unity.EditorCoroutines.Editor; | |
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.Networking; | |
namespace PushNotificationSample.EditorOnly | |
{ | |
/// <summary> | |
/// UnityEditor上からFirebaseのプッシュ通知を試すことが出来るすごいやつ | |
/// | |
/// curl --header "Authorization: key=<Server Key>" --header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d '{"notification":{"title":"Hi","body":"Hello from the Cloud"},"data":{"score":"lots"},"to":"<Registration Token>"}' | |
/// </summary> | |
public class PushNotificationTestWindow : EditorWindow | |
{ | |
/// <summary> | |
/// FirebaseConsole→Setting→CloudMessaging→ServerKeyにある150文字くらいの長い文字列 | |
/// 以前の短いキーは使えない | |
/// </summary> | |
private const string FirebaseServerKey = | |
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; | |
/// <summary> | |
/// Firebase.Messaging.FirebaseMessaging.TokenReceived で取得できるトークン | |
/// 端末ごと、インストールベースごとに決まる | |
/// </summary> | |
private const string TargetDeviceFirebaseToken = | |
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; | |
private EditorCoroutine _coroutine; | |
private string _title = "defaultTitle"; | |
private string _message = "defaultMessage"; | |
[MenuItem("Window/FirebasePushTestingWindow")] | |
static void OpenWindow() | |
{ | |
GetWindow<PushNotificationTestWindow>(); | |
} | |
void OnGUI() | |
{ | |
EditorGUILayout.LabelField("Firebase Local Testing"); | |
EditorGUILayout.BeginHorizontal(); | |
GUILayout.Label("title"); | |
_title = GUILayout.TextField(_title, 25); | |
EditorGUILayout.EndHorizontal(); | |
EditorGUILayout.BeginHorizontal(); | |
GUILayout.Label("body"); | |
_message = GUILayout.TextField(_message, 64); | |
EditorGUILayout.EndHorizontal(); | |
if (GUILayout.Button("Send PushNotification")) | |
{ | |
Debug.Log("Pushed"); | |
_coroutine = EditorCoroutineUtility.StartCoroutineOwnerless(FirebasePushMessageCoroutine(_title, _message)); | |
} | |
EditorGUILayout.Space(); | |
EditorGUILayout.Space(); | |
EditorGUILayout.BeginHorizontal(); | |
GUILayout.Label("ServerKey:"); | |
GUILayout.Label(FirebaseServerKey); | |
EditorGUILayout.EndHorizontal(); | |
EditorGUILayout.BeginHorizontal(); | |
GUILayout.Label("TargetDevice:"); | |
GUILayout.Label(TargetDeviceFirebaseToken); | |
EditorGUILayout.EndHorizontal(); | |
} | |
public IEnumerator FirebasePushMessageCoroutine(string sendingTitle, string sendingMessage) | |
{ | |
FirebaseMessagingBody body = new FirebaseMessagingBody() | |
{ | |
notification = new Notification() | |
{ | |
title = sendingTitle, body = sendingMessage | |
}, | |
data = new NotificationData() | |
{ | |
some = "something" | |
}, | |
to = TargetDeviceFirebaseToken | |
}; | |
using (UnityWebRequest www = FirebasePushMessage(body)) | |
{ | |
var op = www.Send(); | |
while (!www.isDone) | |
{ | |
if (www.isHttpError) | |
{ | |
yield break; | |
} | |
yield return null; | |
} | |
if (www.isNetworkError) | |
{ | |
Debug.Log(www.error); | |
} | |
} | |
} | |
public static UnityWebRequest FirebasePushMessage(FirebaseMessagingBody body) | |
{ | |
//POSTだとContent-Typeが変更できない | |
UnityWebRequest www = | |
new UnityWebRequest("https://fcm.googleapis.com/fcm/send", UnityWebRequest.kHttpVerbPOST); | |
var sendBody = JsonUtility.ToJson(body); | |
Debug.Log($"willSend:{sendBody}"); | |
www.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(sendBody)); | |
www.downloadHandler = new DownloadHandlerBuffer(); | |
www.SetRequestHeader("Authorization", $"key={FirebaseServerKey}"); | |
www.SetRequestHeader("Content-Type", "application/json"); | |
www.chunkedTransfer = false; | |
return www; | |
} | |
} | |
[System.Serializable] | |
public class Notification | |
{ | |
public string title; | |
public string body; | |
} | |
[System.Serializable] | |
public class NotificationData | |
{ | |
public string some; | |
} | |
[System.Serializable] | |
public class FirebaseMessagingBody | |
{ | |
public Notification notification; | |
public NotificationData data; | |
public string to; //token | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment