LoginSignup
4
5

More than 5 years have passed since last update.

【Monaca x mBaaS】 AndroidのLollipop以上:プッシュ通知アイコンの変更方法

Last updated at Posted at 2016-04-19

前提

Monacaで開発していて、Android 5.2以上、プッシュ通知のアイコンの仕様が変わっていますよってご存知の方が多いかと思います。
アイコンが白くなったり、今まできれいに表示してアイコンが急にできなくなったり、多いではないかと思います。
今回その対応をまとめましたので、ぜひご参考になればと思います。
使う前提は 「monaca」と「ニフティクラウドmobile backend (mBaaS)」サービスで説明します。

プッシュ通知プロジェクト

基本的にmonacaもmBaaSも無料サービスであり、プッシュ通知機能もこのドキュメントの通り実施するば、すぐできちゃいます。

→ プッシュ通知の組み込み方

※アプリのアイコン設定しました!
cd754b38-e0dd-2b96-b7ab-95f29191d64c.png

最初にアプリを起動すると、端末登録されます。
ニフティクラウドmobile_backend.png

プッシュ通知を送ってみると(Cordova5.2, push plugin 2.0, Android 6)確かに白くなっています。
1Screenshot_20160419-210318_png_と_CustomReceiver_java_-_AndroidProject201512_-____Documents_mbaas_support-git-201512_android_AndroidProject201512_.png

原因

Android 5.0からの設定
http://developer.android.com/about/versions/android-5.0-changes.html

[Notifications] > [Material design style]

Notifications are drawn with dark text atop white (or very light) backgrounds to match the new material design widgets. Make sure that all your notifications look right with the new color scheme. If your notifications look wrong, fix them:

    Use setColor() to set an accent color in a circle behind your icon image.
    Update or remove assets that involve color. The system ignores all non-alpha channels in action icons and in the main notification icon. You should assume that these icons will be alpha-only. The system draws notification icons in white and action icons in dark gray.

つまり、白くなっているのは仕様ですよ、と色がないアイコンを使うことと、色を変えたいならgetColorで背景色を変えます。
ではこれから色の背景を変える方法を説明します。

手順

  • 手動でpluginをインポート
  • CustomReceiverを追加
  • plugin.xmlを修正

1. 手動でpluginをインポト

  • こちらのURLから「Download zip」 を選択し、ファイルをダウンロードします。
  • Monacaでメニュー「設定」>「Cordovaプラグインの管理」

TestSupport20160328_-_Monaca_Cloud_IDE3.png

TestSupport20160328_-_Monaca_Cloud_IDE.png

上記ダウンロードしたzipを選択し、インポートします。

2. CustomReceiverを追加

Monacaで以下のように新規ファイルを作成します。ファイル名は[CustomReceiver.java]にします。

2TestSupport20160328_-_Monaca_Cloud_IDE.png

CustomReceiver.javaは以下のように実装しています

CustomReceiver.java

package com.example.helloworld; //Your package name

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

public class CustomReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //ペイロードされるデータを抜き取る
        String message = intent.getStringExtra("message") ;

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setSmallIcon(context.getApplicationInfo().icon)
                .setAutoCancel(true)
                .setVibrate(new long[]{0, 200, 100, 200})
                .setColor(0x0FBB12) //背景色を設定
                .setContentTitle("タイトル")
                .setContentText(message);
         NotificationManager notificationManager =
         (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
         notificationManager.notify(-1, builder.build());
    }
  }

3. plugin.xmlを修正

Pluginの設定ファイルを修正します。上記のCustomReceiverを呼ぶように指定する必要あるので!

修正前

            <receiver
                android:name="com.nifty.cloud.mb.core.NCMBGcmReceiver"
                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="$PACKAGE_NAME"/>
                </intent-filter>
            </receiver>

修正後

            <receiver
                android:name="CustomReceiver"
                android:permission="com.google.android.c2dm.permission.SEND" >
                <intent-filter>
                    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                    <category android:name="$PACKAGE_NAME" />
                </intent-filter>
            </receiver>

結果を見てみよう

プッシュ通知を送ってみると、背景色は色ついていました!
2Screenshot_20160419-205552_png_と_CustomReceiver_java_-_AndroidProject201512_-____Documents_mbaas_support-git-201512_android_AndroidProject201512_.png

最後に

難しかったのはアイコンの画像設定はAndroidのようにRの中にあるわけではなく、色々試しながらやった感じでした。
プラグインってかなりblackboxでわからないまま、native code をいじっていましたので、やっとできて、色々勉強になりました!
面白かったのはアイコンはすごくシンプルにはなっていますが、プッシュ通知を長押しすると、アイコンちゃんと表示します!仕様はすごい変わっていますね!さすがAndroid!
今回は試してたので、参考になれば嬉しいです!

4
5
2

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
5