Android - FCM - 收到訊息,顯示Notification通知或顯示Dialog

Android - FCM -  收到訊息,顯示

Notification

通知或客製化訊息視窗

這次要來客製化收到推播後,要顯示Notification或Dialog給使用者知道。

上次的FCM已進行到,能收到推播訊息,

如果是未開啟App的情況下,收到訊息,會直接有Notification,

但開啟App的情況下,會在FcmMessageService 的onMesasgeReceived

而收到的時候,你可以顯示Data或Body,在log查看。

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        }
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            //this.sendNotification(remoteMessage.getNotification());
        }

也可以,送出Notification 

可以將remoteMesage資訊,取得相關資料,顯示Notification


    private void sendNotification(RemoteMessage remoteMessage) {
        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_launcher)
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setContentText(remoteMessage.getNotification().getBody())
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        Random rand = new Random();
        int pushId =  rand.nextInt(50);
        notificationManager.notify(pushId /* ID of notification */, notificationBuilder.build());
    }

或則,顯示Dialog在頁面上

這時就要在Activity註冊或委派事件,

    public void reigsterNotifyReciver() {
        NoticeReceiver.getInstance().setOnMessageReceivedListener(new NoticeReceiver.OnMessageReceivedListener() {
            @Override
            public void onMessageReceived(final String title,final String content) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {                        
                        //Show Dialog
                    }
                });
            }
        });
    }

NoticeReceiver撰寫如下


public class NoticeReceiver {
    private static final NoticeReceiver instance = new NoticeReceiver();

    public static NoticeReceiver getInstance() {
        return instance;
    }

    private NoticeReceiver() {

    }

    private OnMessageReceivedListener mOnMessageReceivedListener;

    public interface OnMessageReceivedListener{
        void onMessageReceived(PushData data);
    }

    public void setOnMessageReceivedListener(OnMessageReceivedListener listener){
        mOnMessageReceivedListener = listener;
    }
    public void notifyOnMessageReceived(PushData data){
        if(mOnMessageReceivedListener != null){
            mOnMessageReceivedListener.onMessageReceived(data);
        }
    }
}

客製化視窗,請查過過去撰寫的文章即可。