跳到主要内容

Mobage Native SDK ( Android ) 1.7.0 以降でリモート通知を利用する場合

Mobage Native SDK (Android) 1.7.0 未満から、 Mobage Native SDK (Android) 1.7.0 以降へ変更する場合、かつリモート通知を利用する際には本ページに記載する内容の変更が必要になります。

AndroidManifest.xml の変更​

  • タグの外に記載されている以下の要素を削除します。
<!-- Only this application can receive the messages and registration result -->
<permission android:name="{TODO: replace with your package name}.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="{TODO: replace with your package name}.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive message -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
  • タグの外の以下の要素は必要ですので削除しないでください。
<uses-permission android:name="android.permission.INTERNET" />
  • タグ内に記載された以下の項目をすべて削除してください。
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="jp.mbga.a${YourApplicationId}" />
</intent-filter>
</receiver>
<service
android:name="com.mobage.android.gcm.MessageListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.mobage.android.gcm.IIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
  • タグ内に以下を記載してください。
<provider
android:name="com.google.firebase.provider.FirebaseInitProvider"
android:authorities="${applicationId}.firebaseinitprovider"
tools:node="remove"/>

 
<service
android:name="com.mobage.android.fcm.MessageListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="com.mobage.android.fcm.RemoteNotificationController$FcmMobageJobService"
android:exported="false">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>
  • タグに設定されたアイコンの設定に関しては、以下のニュース記事に記載されているように、 <application android:icon="@drawable/icon" に設定しなくとも良くなりました。

  • リモート通知には、 rn_icon.png を通知アイコンとして使用されます。drawable の下に rn_icon.png を設置してください。

注意

drawable/rn_icon.png が設置されていないとリモート通知が通知されません。

  • AndroidManifest.xml の タグ内の Main Activity の launchMode は、以下のように設定してください。
    • android:launchMode="standard"  又は android:launchMode="singleTop"

build.gradle の変更​

以下の項目をアプリケーションの build.gradle へ追加します。 firebase-messaging は 17.1.0 以上、 firebase-jobdispatcher は 0.8.5 以上がサポート対象になります。

dependencies{
implementation 'com.google.firebase:firebase-messaging:17.1.0'
implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
}
注意

上記では、implementation を利用していますが、以下の各種 build.gradle に記載された項目は compile のままでないとビルドエラーとなりますのでご注意ください。

アプリケーションの build.gradle

dependencies{
compile project(':mobageNativeAndroid')
}

mobageNativeAndroid の build.gradle

dependencies {
compile files('libs/MobageNativeSdk.jar')
}

FCM 初期化について​

Mobage.initialize() を呼び出したあと、必ず Mobage.registerForRemoteNotification を呼び出してください。(ただし、Firebase をパートナー様においてもご利用される場合は、設定が異なります)

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Mobage.initialize(
Mobage.Region.JP,
Mobage.ServerMode.SANDBOX,
"${ANDROID_SDK_CONSUMER_KEY}",
"${ANDROID_SDK_CONSUMER_SECRET}",
"${MOBAGE_APPLICATION_ID}",
this,
true);
Mobage.registerForRemoteNotification(); // 追加してください
Mobage.onCreate(this);
setMobagePlatformListener();
// checkLoginStatus should be invoked only when launching app
Mobage.checkLoginStatus();
}

Firebase の機能をアプリ内で利用する場合​

Mobage Native / Unity SDK 以外で、アプリ内で Firebase (Realtime Database 等含む) を利用される場合、Mobage.registerForRemoteNotification() に与える引数が異なることがあります。

  1. アプリ側で Firebase の初期化 ([DEFAULT] での初期化)を Mobage.registerForRemoteNotification() 呼び出しよりも前に行う場合
    1. Mobage.registerForRemoteNotification(true) を呼び出します。
  2. アプリ側で Firebase の初期化を Mobage.registerForRemoteNotification() 呼び出しよりも後に行う場合
    1. Mobage.registerForRemoteNotification() を呼び出します。
    2. こちらのリンクに従い、 FirebaseApp.initializeApp での初期化時の第三引数に、アプリ側で決定したインスタンス名を指定して初期化してください。

Native Android で音や振動を鳴らす場合に使用する RemoteNotificationClient クラスの変更​

リモート通知の使い方 に記載されている「Native Android で音や振動を鳴らしたい場合 (Mobage Native SDK 1.4.7 以降)」について行う場合、RemoteNotificationClient が以下のように変更になりましたので注意が必要です。

public boolean handleRegistration(Context context, Intent intent) {
↓修正
public boolean handleRegistration(Context context, String registrationId) {
public static class RemoteNotificationClient {
/**
* Give the host application a chance to take over the control when a registration id is about to be received in the application.
* If the host application wants to handle the registration id, return true, otherwise return false.
* @param context The Context in which the remote notification receiver is running.
* @param registrationId The String being received.
* @return True if the host application wants to handle the registration intent itself, otherwise return false.
*/
public boolean handleRegistration(Context context, String registrationId) { 
return false;
}

/**
* Give the host application a chance to take over the control when a remote notification message intent is about to be received in the application.
* If the host application wants to handle the message intent and display notification, return true, otherwise return false.
* @param context The Context in which the remote notification receiver is running.
* @param intent The Intent being received.
* @return True if the host application wants to handle the message intent itself, otherwise return false.
*/
public boolean handleMessage(Context context, Intent intent) {
return false;
}

/**
* Give the host application a chance to tweak the Notification instance before the notification is displayed by the SDK.
* @param notification The Notification instance that is created and will be display by the SDK.
* @param context The Context in which the remote notification receiver is running.
* @param intent The Intent being received.
*/
public void tweakNotification(Notification notification, Context context, Intent intent) {
return;
}
}

Proguard 設定の変更​

Proguard を利用する場合、以下の項目を proguard.cfg や proguard-android.txt へ設定してください。

-keep class com.mobage.android.** { *; }
-keep class jp.dena.mobage.android.** { *; }
-keep class com.android.vending.billing.** { *; }
-keep class android.support.v4.** { *; }
-keep class com.google.android.gms.** { *; }
-keep class jp.dena.android.http.util.SerializableCookie { *; }
 
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
public static final *** NULL;
}
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
@com.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}

-keep class com.firebase.jobdispatcher.** { *; }
-keep class com.google.firebase.** { *; }

引き続き設定が必要な項目​

以下の項目については、削除せずにそのまま残してください。(以前からある項目です)

  • Main の Activity クラスに以下のようなフラグを追加します。
private boolean started = false;
  • Main の Activity の onCreate() 内に以下のように追記します。
RemoteNotification.setListener(new RemoteNotificationListener() {
public void handleReceive(Context context, Intent intent) {
RemoteNotificationPayload payload = RemoteNotification.extractPayloadFromIntent(intent);
Log.i("---------------", "#### " + payload.getMessage());
RemoteNotification.displayStatusBarNotification(context, intent);
}
});
if(!started) {
handleIntent(getIntent());
started = true;
}
  • Main の Activity クラスで以下のように onNewIntent() をオーバーライドし、handleIntent()を呼び出します。
@Override
protected void onNewIntent (Intent intent){
Log.i(TAG, "onNewIntent");
handleIntent(intent);
}
  • Main の Activity クラス内に以下の handleIntent() を追加します。アプリケーションが起動していない場合、バックグラウンドの場合は、通知領域でメッセージをタップした時に以下のメソッドが呼ばれます
private void handleIntent(Intent intent){
if (intent != null) {
setIntent(intent);
Bundle extras = intent.getExtras();
if(extras == null){
Log.i(TAG, "Intent extras is empty");
}else{
String message = extras.getString("message");
String extrasJsonString = extras.getString("extras");
Log.i(TAG, "message" + message);
}
}
}
  • ログイン後、早いタイミングで RemoteNotification.setRemoteNotificationEnabled() を呼ぶ必要があります。
RemoteNotification.setRemoteNotificationsEnabled(true, new OnSetRemoteNotificationsEnabledComplete() {
@Override
public void onSuccess() {
/* flag was set properly */
}

@Override
public void onError(Error error) {
/* An error occurred in setting this value */
}
});

更新履歴​

  • 2018/11/14
    • 新規作成