Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
remove onNewToken() event in ncmb fcm plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
vfa-thanhnh committed Nov 23, 2018
1 parent 04f74ed commit d932d64
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 74 deletions.
1 change: 0 additions & 1 deletion NcmbFcmPlugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ buildscript {

allprojects {
repositories {
jcenter()
google()
}
}
Binary file modified NcmbFcmPlugin/ncmbfcmplugin/release/ncmbfcmplugin.aar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.tasks.OnCanceledListener;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.iid.FirebaseInstanceId;
Expand All @@ -40,6 +41,12 @@

//FCMの初期化処理を扱います
public class FCMInit extends Activity {
private static final String CANNOT_GET_DEVICE_TOKEN_MESSAGE =
"Can not get device token, please check your google-service.json";
private static final String NOT_SUPPORT_PLAY_SERVICE_MESSAGE =
"This device is not supported google-play-services-APK.";
private static final String CAN_NOT_GET_TOKEN_MESSAGE =
"Can not get device token";

public static void Init(){

Expand All @@ -51,42 +58,59 @@ public static void Init(){
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if(task.isSuccessful()) {
final String token = task.getResult().getToken();
//Setting chanel for Android O
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NCMBNotificationUtils utils = new NCMBNotificationUtils(UnityPlayer.currentActivity);
utils.settingDefaultChannels();
if(task.isSuccessful()) {
final String token = task.getResult().getToken();
//Setting chanel for Android O
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NCMBNotificationUtils utils = new NCMBNotificationUtils(UnityPlayer.currentActivity);
utils.settingDefaultChannels();
}
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
public void run() {
UnityPlayer.UnitySendMessage("NCMBManager", "onTokenReceived", token);
}
});
} else{
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
public void run() {
UnityPlayer.UnitySendMessage("NCMBManager", "OnRegistration", CAN_NOT_GET_TOKEN_MESSAGE);
}
});
}
}
});

FirebaseInstanceId.getInstance().getInstanceId().addOnCanceledListener(new OnCanceledListener() {
@Override
public void onCanceled() {
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
public void run() {
UnityPlayer.UnitySendMessage("NCMBManager", "onTokenReceived", token);
UnityPlayer.UnitySendMessage("NCMBManager", "OnRegistration", CAN_NOT_GET_TOKEN_MESSAGE);
}
});
} else{
}
});

FirebaseInstanceId.getInstance().getInstanceId().addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
public void run() {
UnityPlayer.UnitySendMessage("NCMBManager", "OnRegistration", "Can not get token");
UnityPlayer.UnitySendMessage("NCMBManager", "OnRegistration", CAN_NOT_GET_TOKEN_MESSAGE);
}
});
}
}
});

FirebaseInstanceId.getInstance().getInstanceId().addOnCanceledListener(new OnCanceledListener() {
@Override
public void onCanceled() {
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
public void run() {
UnityPlayer.UnitySendMessage("NCMBManager", "OnRegistration", "Can not get token");
}
});
} else {
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
public void run() {
UnityPlayer.UnitySendMessage("NCMBManager", "OnRegistration", CANNOT_GET_DEVICE_TOKEN_MESSAGE);
}
});
}

} else {
UnityPlayer.UnitySendMessage("NCMBManager", "OnRegistration", "This device is not supported google-play-services-APK.");
UnityPlayer.UnitySendMessage("NCMBManager", "OnRegistration", NOT_SUPPORT_PLAY_SERVICE_MESSAGE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,6 @@ public class NCMBFirebaseMessagingService extends FirebaseMessagingService {
private final String TAG = "NCMBFirebaseMessagingService";
public static final String NS = "NCMB_SPLITTER";

/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
@Override
public void onNewToken(String token) {
// Send refesh token to update installation
final String saveToken = token;
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
public void run() {
UnityPlayer.UnitySendMessage("NCMBManager", "onTokenReceived", saveToken);
}
});
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

Expand Down
43 changes: 24 additions & 19 deletions ncmb_unity/Assets/NCMB/NCMBManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@ public virtual void Awake ()
void OnRegistration (string message)
{
Inited = true;


//Exceute NCMBDeviceTokenCallbackQueue
if(message != null && message.Length > 0){
NCMBDeviceTokenCallbackQueue.GetInstance().execQueue(null,
new NCMBException (new Exception (message)));
} else {
NCMBDeviceTokenCallbackQueue.GetInstance().execQueue(_token, null);
}
if (onRegistration != null) {
if (message == "") {
message = null;
Expand Down Expand Up @@ -227,7 +234,6 @@ internal static string SearchPath ()
}
}


//ネイティブでデバイストークン取得後に呼び出されます
internal void onTokenReceived (string token)
{
Expand All @@ -250,6 +256,7 @@ internal void onTokenReceived (string token)
installation.SaveAsync ((NCMBException saveError) => { //更新実行
if (saveError != null) {
//対処可能なエラー
OnRegistration(saveError.ErrorMessage);
if (saveError.ErrorCode.Equals(NCMBException.DUPPLICATION_ERROR)){
//過去に登録したデバイストークンと衝突。アプリの再インストール後などに発生
updateExistedInstallation (installation, path);
Expand Down Expand Up @@ -277,23 +284,21 @@ private void updateExistedInstallation (NCMBInstallation installation, string pa
{
//デバイストークンを更新
NCMBQuery<NCMBInstallation> query = NCMBInstallation.GetQuery (); //ObjectId検索
installation.GetDeviceToken((token, error) => {
query.WhereEqualTo("deviceToken", token);
query.FindAsync ((List<NCMBInstallation> objList, NCMBException findError) => {
if (findError != null) {
OnRegistration (findError.ErrorMessage);
} else if (objList.Count != 0) {
installation.ObjectId = objList [0].ObjectId;
installation.SaveAsync ((NCMBException installationUpdateError) => {
if (installationUpdateError != null) {
OnRegistration (installationUpdateError.ErrorMessage);
} else {
OnRegistration ("");
}
});
}
});
});
query.WhereEqualTo("deviceToken", _token);
query.FindAsync ((List<NCMBInstallation> objList, NCMBException findError) => {
if (findError != null) {
OnRegistration (findError.ErrorMessage);
} else if (objList.Count != 0) {
installation.ObjectId = objList [0].ObjectId;
installation.SaveAsync ((NCMBException installationUpdateError) => {
if (installationUpdateError != null) {
OnRegistration (installationUpdateError.ErrorMessage);
} else {
OnRegistration ("");
}
});
}
});
}

//ディスク入出力関数
Expand Down
75 changes: 75 additions & 0 deletions ncmb_unity/Assets/NCMB/Script/NCMBDeviceTokenCallbackQueue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*******
Copyright 2017-2018 FUJITSU CLOUD TECHNOLOGIES LIMITED All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**********/

using System;
using NCMB.Internal;
using UnityEngine;
using System.Collections.Generic;

namespace NCMB
{
public class NCMBDeviceTokenCallbackQueue
{
private static NCMBDeviceTokenCallbackQueue instance;

Queue<NCMBGetCallback<String>> queue;
public static NCMBDeviceTokenCallbackQueue GetInstance()
{
if (instance == null)
{
instance = new NCMBDeviceTokenCallbackQueue();
}
return instance;
}

public Boolean isDuringSaveInstallation()
{
return (queue != null);
}

void beginSaveInstallation()
{
if (queue == null)
{
queue = new Queue<NCMBGetCallback<string>>();
}
}
public void addQueue(NCMBGetCallback<string> callback)
{
beginSaveInstallation();
queue.Enqueue(callback);
}

public void execQueue(String token, NCMBException e)
{
if (queue == null)
{
return;
}
while (queue.Count > 0)
{
NCMBGetCallback<string> callback = queue.Dequeue();
callback(token, e);
}

endSaveInstallation();
}
void endSaveInstallation()
{
queue = null;
}
}
}
34 changes: 16 additions & 18 deletions ncmb_unity/Assets/NCMB/Script/NCMBInstallation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,25 +131,23 @@ public string DeviceToken {
/// </summary>
/// <param name="callback">コールバック</param>
public void GetDeviceToken(NCMBGetCallback<String> callback){
if(this.ContainsKey("deviceToken") && this["deviceToken"] != null ){
if (NCMBDeviceTokenCallbackQueue.GetInstance().isDuringSaveInstallation())
{
NCMBDeviceTokenCallbackQueue.GetInstance().addQueue(callback);
return;
}

if (this.ContainsKey("deviceToken") && this["deviceToken"] != null)
{
callback((string)this["deviceToken"], null);
} else {
new Thread(() => {
for (int i = 0; i < 10; i++){
if (NCMBManager._token != null){
this["deviceToken"] = NCMBManager._token;
break;
}
Thread.Sleep(500);
}
if (callback != null){
if (this.ContainsKey("deviceToken") && this["deviceToken"] != null){
callback((string)this["deviceToken"], null);
} else {
callback(null, new NCMBException("Can not get device token"));
}
}
}).Start();
return;
}
else
{
NCMBDeviceTokenCallbackQueue.GetInstance().addQueue(callback);
#if UNITY_ANDROID
NCMBPush.Register();
#endif
}
}

Expand Down
Binary file modified ncmb_unity/Assets/Plugins/Android/ncmbfcmplugin.aar
Binary file not shown.

0 comments on commit d932d64

Please sign in to comment.