[Firebase] 04 - Push Notification

From: Push Notification Introduction - Android Firebaseandroid

記得更新google-services.json文件。git

 

發送Message模擬器:github

 

而後是兩個問題:json

 


 

1. 首先,它是一個服務:ide

<service
  android:name=".FirebaseMessagingService">
  <intent-filter>
    <action android:name="com.google.firebase.MESSAGING_EVENT"/>
  </intent-filter>
</service>

服務代碼:函數

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService  {
    private static final String TAG = "FirebaseMessagingServic";
    public FirebaseMessagingService() {
    }

@Override
public void onMessageReceived(RemoteMessage remoteMessage) { String title = remoteMessage.getNotification().getTitle(); String message = remoteMessage.getNotification().getBody();
Log.d(TAG,
"onMessageReceived: Message Received: \n" + "Title: " + title + "\n" + "Message: " + message); sendNotification(title,message);  // --> } @Override public void onDeletedMessages() { } private void sendNotification(String title,String messageBody) {
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.mipmap.ic_launcher) .setContentTitle(title) .setContentText(messageBody) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); } }

 

2. 收到json格式的消息,解析之,點擊跳入指定的頁面(activity):ui

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {  // 函數基本重寫
// Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); try { JSONObject data = new JSONObject(remoteMessage.getData()); String jsonMessage = data.getString("extra_information"); Log.d(TAG, "onMessageReceived: \n" + "Extra Information: " + jsonMessage); } catch (JSONException e) { e.printStackTrace(); } } // Check if message contains a notification payload. if (remoteMessage.getNotification() != null) { String title = remoteMessage.getNotification().getTitle(); //get title String message = remoteMessage.getNotification().getBody(); //get message String click_action = remoteMessage.getNotification().getClickAction(); //get click_action Log.d(TAG, "Message Notification Title: " + title); Log.d(TAG, "Message Notification Body: " + message); Log.d(TAG, "Message Notification click_action: " + click_action); sendNotification(title, message, click_action); } }

 

Body內容this

Json格式消息google

if-else指定跳入的activity:spa

    private void sendNotification(String title, String messageBody, String click_action) {
Intent intent;
if(click_action.equals("SOMEACTIVITY")){ intent = new Intent(this, SomeActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); } else if(click_action.equals("MAINACTIVITY")){ intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); }else{ intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); }

... ...
}

 

Headers內容

Project Overview --> Setttings --> CLOUD MESSAGING --> "Server key"

 

 

3. 根據message的topic進行消息推送

判斷消息的topic代碼:

import com.google.firebase.messaging.FirebaseMessaging;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FirebaseMessaging.getInstance().subscribeToTopic("NEWYORK_WEATHER");
        //FirebaseMessaging.getInstance().unsubscribeFromTopic("NEWYORK_WEATHER");
    }
}

若是接收消息,則繼續執行。

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); } // Check if message contains a notification payload. if (remoteMessage.getNotification() != null) { String title = remoteMessage.getNotification().getTitle(); //get title String message = remoteMessage.getNotification().getBody(); //get message Log.d(TAG, "Message Notification Title: " + title); Log.d(TAG, "Message Notification Body: " + message); sendNotification(title, message); } }
相關文章
相關標籤/搜索