-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyFirebaseInstanceIdService.java
50 lines (44 loc) · 2.06 KB
/
MyFirebaseInstanceIdService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package example.com.kii.firebasestudy;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
import com.kii.cloud.storage.Kii;
import com.kii.cloud.storage.KiiCallback;
import com.kii.cloud.storage.KiiUser;
import com.kii.cloud.storage.callback.KiiPushCallBack;
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
public static final String TAG = "MyIDService";
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
// Get updated InstanceID token.
final String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
final SharedPreferences spf = this.getSharedPreferences("FCM", 0);
spf.edit().putString("FCMToken", refreshedToken).apply();
Kii.initialize(this.getApplicationContext(), Config.APP_ID, Config.APP_KEY, Config.APP_SITE);
// Try to login with Stored Credentials if there's user sign up/in to Kii Cloud.
KiiUser.loginWithStoredCredentials(new KiiCallback<KiiUser>() {
@Override
public void onComplete(KiiUser kiiUser, Exception e) {
if (e != null) {
Log.v(TAG, "Failed to login to Cloud or No user logged in.");
// Don't worry. Push will be installed when user sign up/in to Kii Cloud on MainActivity.
return;
}
KiiUser.pushInstallation().install(refreshedToken, new KiiPushCallBack() {
@Override
public void onInstallCompleted(int taskId, @Nullable Exception e) {
if (e != null) {
Log.v(TAG, "Failed to Install push");
return;
}
Log.v(TAG, "Push Installation succeeded.");
}
});
}
});
}
}