Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

จะใช้การส่งข้อความ firebase ในแอปพลิเคชัน Android ได้อย่างไร?


ตัวอย่างนี้สาธิตวิธีใช้ข้อความ firebase ในแอปพลิเคชัน Android

ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่

ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน src/MainActivity.java

import android.os.Bundle;import android.support.v4.app.FragmentActivity;คลาสสาธารณะ MainActivity ขยาย FragmentActivity { @Override public void onCreate( Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); }}

ขั้นตอนที่ 3 − เพิ่มรหัสต่อไปนี้ใน src/ MyFirebaseMessagingService.java

<รุ่นก่อนหน้า>นำเข้า android.app.Notification;นำเข้า android.app.NotificationChannel;นำเข้า android.app.NotificationManager;นำเข้า android.content.Context;นำเข้า android .graphics.Color;นำเข้า android.os.Build;นำเข้า android.support.v4.app.NotificationCompat;นำเข้า android.support.v4.content.ContextCompat;นำเข้า android.util.Log;import com.google.firebase.messaging FirebaseMessagingService;import com.google.firebase.messaging.RemoteMessage;import org.json.JSONObject;import java.util.Map; MyFirebaseMessagingService คลาสสาธารณะ ขยาย FirebaseMessagingService { @Override public void onNewToken (String s) { Log.e ("NEW" , s); } @แทนที่โมฆะสาธารณะ onMessageReceived (RemoteMessage remoteMessage) { Map params =remoteMessage.getData (); วัตถุ JSONObject =JSONObject ใหม่ (พารามิเตอร์); Log.e("JSON_OBJECT", object.toString()); สตริง NOTIFICATION_CHANNEL_ID ="sairam"; แบบยาว[] ={0, 1000, 500, 1000}; NotificationManager mNotificationManager =(NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE); ถ้า (Build.VERSION.SDK_INT> =Build.VERSION_CODES.O) { ช่องการแจ้งเตือนช่องแจ้งเตือน =ช่องการแจ้งเตือนใหม่ (NOTIFICATION_CHANNEL_ID, "การแจ้งเตือนของคุณ", NotificationManager.IMPORTANCE_HIGH); alertChannel.setDescription(""); alertChannel.enableLights(จริง); alertChannel.setLightColor(สี.แดง); alertChannel.setVibrationPattern(รูปแบบ); alertChannel.enableVibration(จริง); mNotificationManager.createNotificationChannel (ช่องแจ้งเตือน); } // เพื่อแสดงการแจ้งเตือนในโหมด DND ถ้า (Build.VERSION.SDK_INT> =Build.VERSION_CODES.O) { ช่องการแจ้งเตือน =mNotificationManager.getNotificationChannel (NOTIFICATION_CHANNEL_ID); channel.canBypassDnd(); } NotificationCompat.Builder notificationBuilder =ใหม่ NotificationCompat.Builder (นี่คือ NOTIFICATION_CHANNEL_ID); alertBuilder.setAutoCancel(จริง) .setColor(ContextCompat.getColor(นี่ R.color.colorAccent)) .setContentTitle(getString(R.string.app_name)) .setContentText(remoteMessage.getNotification().getBody()) .setDefaults( การแจ้งเตือน DEFAULT_ALL) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_launcher_background) .setAutoCancel(true); mNotificationManager.notify(1000, notificationBuilder.build()); }}

มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก จากนั้นตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ –

จะใช้การส่งข้อความ firebase ในแอปพลิเคชัน Android ได้อย่างไร?