如何在 Android 應用程式中使用 firebase 訊息傳遞?


此示例演示瞭如何在 Android 應用程式中使用 firebase 訊息傳遞

步驟 1 − 在 Android Studio 中建立一個新專案,轉到檔案 ⇒ 新建專案,並填寫所有必需的詳細資訊以建立一個新專案。

步驟 2 − 將以下程式碼新增到 src/MainActivity.java

<?xml version = "1.0" encoding = "utf-8"?>
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
}

步驟 3 − 將以下程式碼新增到 src/ MyFirebaseMessagingService.java

<?xml version = "1.0" encoding = "utf-8"?>
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONObject;
import java.util.Map;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
   @Override
   public void onNewToken(String s) {
      Log.e("NEW_TOKEN", s);
   }
   @Override
   public void onMessageReceived(RemoteMessage remoteMessage) {
      Map<String, String> params = remoteMessage.getData();
      JSONObject object = new JSONObject(params);
      Log.e("JSON_OBJECT", object.toString());
      String NOTIFICATION_CHANNEL_ID = "sairam";
      long pattern[] = {0, 1000, 500, 1000};
      NotificationManager mNotificationManager =
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.O) {
         NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications",
         NotificationManager.IMPORTANCE_HIGH);
         notificationChannel.setDescription("");
         notificationChannel.enableLights(true);
         notificationChannel.setLightColor(Color.RED);
         notificationChannel.setVibrationPattern(pattern);
         notificationChannel.enableVibration(true);
         mNotificationManager.createNotificationChannel(notificationChannel);
      }
      // to diaplay notification in DND Mode
      if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.O) {
         NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
         channel.canBypassDnd();
      }
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
      notificationBuilder.setAutoCancel(true)
      .setColor(ContextCompat.getColor(this, R.color.colorAccent))
      .setContentTitle(getString(R.string.app_name))
      .setContentText(remoteMessage.getNotification().getBody())
      .setDefaults(Notification.DEFAULT_ALL)
      .setWhen(System.currentTimeMillis())
      .setSmallIcon(R.drawable.ic_launcher_background)
      .setAutoCancel(true);
      mNotificationManager.notify(1000, notificationBuilder.build());
   }
}

讓我們嘗試執行應用程式。我假設你已將安卓移動裝置連線至電腦。從 Android Studio 執行應用程式,開啟專案的一個 activity 檔案,並點選工具欄中的執行  圖示。選擇移動裝置作為選項,然後檢視顯示預設螢幕的移動裝置 –

點選 此處 下載專案程式碼

更新時間: 2019 年 7 月 30 日

342 瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

立即開始
廣告