4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

デバイストークン取得(Unity)

Last updated at Posted at 2017-05-29

1:Apple Developer Program から プッシュ通知サービスのチェックを付けて、AppIDsを作成
2:1で作成したAppIDsを使ってプロビジョニングプロファイルを作成
3:Unityを開き、1で作成したバンドルIDをセットする。
4:下記コードを実装してボタンから呼べるようにする。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GetToken : MonoBehaviour {

	// Use this for initialization
	void Start () {

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

	public void GetTokenString()
	{
		GetTokenData ();
	}


	private void GetTokenData ()
	{
		#if UNITY_IPHONE

		string deviceToken = "";
		// デバイストークン取得
		byte[] token = UnityEngine.iOS.NotificationServices.deviceToken;
		if (token != null)
		{
			deviceToken = System.BitConverter.ToString(token).Replace("-", "");
		}
		else
		{
			deviceToken = "Nothing";
		}

		// ベンダーID取得
		string identifierForVendor = UnityEngine.iOS.Device.vendorIdentifier;

		Debug.Log("deviceToken=" + deviceToken);
		Debug.Log("identifierForVendor=" + identifierForVendor);

		#endif

	}
}

 
5:ビルドする。
6:Xcodeを開いて、Capabilitiesタブを選択して Push Notifications を ON にする。
7:Generalタブを選択して、SingingのAutomatically manage sigining のチェックを外す
8:Signing(Release)等のプロビジョニングプロファイルの設定を2で作成したものに設定する。
9:実行する。
10:実機にて通知を許可するか聞かれるので、「はい」を選択
11:ボタンをタップして、Xcodeのログにデバイストークンが表示される事を確認

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?