LoginSignup
4
2

More than 5 years have passed since last update.

【mBaaS】プッシュ通知のメッセージを改行する【Android】

Last updated at Posted at 2017-06-22

mBaaSのプッシュ通知を利用した時、メッセージに入れた改行がiOSではそのまま反映されますが、Androidの場合は改行が無視されて表示されてしまいます。

しかし、受け取ったプッシュ通知の情報には、改行のところが「\n」として保持されているので、この情報をもとにNotificationのInboxStyleを使って複数行の表示に変換します。

実装

新しくクラスを作成し、SDKの既存のNCMBGcmListenerServiceを継承します。
InboxStyleでメッセージを変更している部分以外は元のクラスとほぼ同じです。

CustomGcmListenerService.java
public class CustomGcmListenerService extends NCMBGcmListenerService {
    @Override
    public void onMessageReceived(String from, Bundle data) {

        if ((!data.containsKey("message")) && (!data.containsKey("title"))) {
            return;
        }

        // 親Serveiceからプッシュ通知情報を取得
        NotificationCompat.Builder notificationBuilder = notificationSettings(data);

        // ---------ここからInboxStyleで表示を改変---------

        // InboxStyle:複数行のテキストを表示
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(notificationBuilder);

        // タイトル(そのまま)
        if(null != data.getString("title")) {
            String title = data.getString("title");
            inboxStyle.setBigContentTitle(title);
        }

        // メッセージ(InboxStyleを利用して改行)
        if(null != data.getString("message")) {
            String message = data.getString("message");

            // 改行文字列をsplitで分割
            String[] messageArray = message.split("\n");

            // 分割された文字列を行として追加
            for (int i=0; i < messageArray.length; i++) {
                inboxStyle.addLine(messageArray[i]);
            }
        }

        // ---------ここまでInboxStyleで表示を改変---------

        /*
         * 通知重複設定
         * 0:常に最新の通知のみ表示
         * 1:最新以外の通知も複数表示
         */
        ApplicationInfo appInfo = null;
        try {
            appInfo = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            throw new IllegalArgumentException(e);
        }
        boolean containsKey = appInfo.metaData.containsKey("notificationOverlap");
        int overlap = appInfo.metaData.getInt("notificationOverlap");

        //デフォルト複数表示
        int notificationId = new Random().nextInt();

        if (overlap == 0 && containsKey) {
            //最新のみ表示
            notificationId = 0;
        }

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(notificationId, inboxStyle.build());

    }
}

またマニフェストも新しく作成したサービスを宣言します。

AndroidManifest.xml
<!--
<service
    android:name="com.nifty.cloud.mb.core.NCMBGcmListenerService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
    </intent-filter>
</service>
-->

<service
    android:name="パッケージ名.CustomGcmListenerService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
    </intent-filter>
</service>

結果

  • 作成したプッシュ通知

  • 受け取ったプッシュ通知

4
2
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
2