如何在 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 文件,并点击工具栏中的运行 图标。选择移动设备作为选项,然后查看显示默认屏幕的移动设备 –
点击 此处 下载项目代码
广告