moshimore Knowledge

アプリ開発とメモ書き、日々の日記。

コピペでFirebase Cloud Messagingを使う方法

f:id:moshimore:20170731015501j:plain

サクッと、コピペでFCMが使えるようにメモ書きです。


こちらのサイトを参考にどんどんコピペしてきます。

gihyo.jp

build.gradle

dependencies
{
    compile 'com.google.firebase:firebase-messaging:9.2.0'
}

MyFirebaseInstanceIDService.java

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

import static android.content.ContentValues.TAG;

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService
{
    @Override
    public void onTokenRefresh() {
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);
    }
}

MyFirebaseMessagingService.java

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.util.Map;

import static android.content.ContentValues.TAG;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());

        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Notification Message");
            String title = remoteMessage.getNotification().getTitle();
            Log.d(TAG, "Notification Message Title: " + title);
            String body = remoteMessage.getNotification().getBody();
            Log.d(TAG, "Notification Message Body: " + body);
        }

        if (remoteMessage.getData() != null) {
            Log.d(TAG, "Data Message");
            Map<String, String> data = remoteMessage.getData();
            String customTitle = data.get("custom_title");
            Log.d(TAG, "Notification Message Title: " + customTitle);
            String customBody = data.get("custom_body");
            Log.d(TAG, "Notification Message Body: " + customBody);

            Log.d(TAG, "Data Message");
            Map<String, String> data2 = remoteMessage.getData();
            String title = data2.get("custom_title");
            String body = data2.get("custom_body");

            sendNotification(title, body);

        }
    }

    private void sendNotification(String title, String body) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle(title)
                .setContentText(body)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

AndroidManifest.xml

<application>
    <service
        android:name=".MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
    <service
        android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
</application>