kali ini kita akan mencoba mengirimkan Push Notification Firebase dari server (PHP) ke aplikasi android firebase sendiri sudah menyiapkan link/endpoint untuk mengirimkan push notification secara instan dengan mengisi semua parameter headernya dan bodynya yaitu https://fcm.googleapis.com/fcm/send kalian bisa membaca detailnya disini ok pertama buat file php seterah dengan nama apa yang penting berada di dalam folder local server(XAMPP/MAMP/LAMP/LARAGON) untuk merequest ke endpoint FCM-nya kita akan memanfaatkan method curl yang ada di php <?php function sendPushNotification($fcm_token, $title, $message, $id = null,$action = null) { $url = "https://fcm.googleapis.com/fcm/send"; $header = [ 'authorization: key=<TOKEN_MU>', 'content-type: application/json' ]; $notification = [ 'title' =>$title, 'body' => $message ]; $extraNotificationData = ["message" => $notification,"id" =>$id,'action'=>$action]; $fcmNotification = [ 'to' => $fcm_token, 'notification' => $notification, 'data' => $extraNotificationData ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification)); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); $result = curl_exec($ch); curl_close($ch); return $result; } isi <TOKEN_MU> dengan token firebasemu yang bisa didapatkan lewat halaman console firebase kemudian kita langsung panggil aja fungsinya <?php sendPushNotification("<FCM_TOKEN_DARI_APLIKASI_ANDROID>","Ini Title","Ini Isi",123456,"notif") kalian bisa mendapatkan fcm token dari generate di aplikasi androidnya menggunakan library firebase-nya jadi alurnya jika user mendaftar 1.user mendaftar 2.aplikasi android menggenerate fcm token 3.aplikasi mengirim data user dan fcm token yang sudah di generate 4.lalu kedua data tersebut di simpan di database 5.kemudian kita tinggal pake fcm token yang ada di database untuk digunakan mengirimkan notifikasi ke user tersebut kode untuk menggenerate fcm token contohnya seperti ini menggunakan bahasa kotlin FirebaseInstanceId.getInstance().instanceId .addOnCompleteListener(OnCompleteListener { task -> if (task.isSuccessful) { val fcm_token = task.result?.token.toString() } else { // gagal } }) kalian bisa melihat detail lengkapnya disini lalu untuk menerimanya pesannya gimana? kamu tinggal membuat class baru dengan FirebaseMessagingService class MyFirebaseInstanceIdService : FirebaseMessagingService() { val TAG = "PushNotifService" lateinit var name: String private var broadcaster: LocalBroadcastManager? = null override fun onCreate() { broadcaster = LocalBroadcastManager.getInstance(this) } override fun onMessageReceived(remoteMessage: RemoteMessage) { super.onMessageReceived(remoteMessage) handleMessage(remoteMessage) } private fun handleMessage(remoteMessage: RemoteMessage) { val handler = Handler(Looper.getMainLooper()) handler.post(Runnable { val intent = Intent("MyData") remoteMessage.notification?.let { intent.putExtra("message", it.body) intent.putExtra("title", it.title) intent.putExtra("id", remoteMessage.data["id"]) intent.putExtra("action", remoteMessage.data["action"]) broadcaster?.sendBroadcast(intent) } } ) } } lalu tinggal mendaftarkan servicenya ke AndroidManifest.xml <service android:name=".MyFirebaseInstanceIdService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> kalian bisa melihat detailnya disini PHP is a server side process - it only runs when a user/app/service requests the page. MySQL is a server based database. It's not "connected" to your app. Your app must then "poll" your website (it has to request data from it periodically). This is best done using an XMPP type service. You can find a lot of info on XMPP - basically, it's complicated to setup and run. It probably isn't worth doing unless you've already done it. Alternatively, you can use an existing XMPP service. Google Cloud Messaging (GCM) is an excellent option for most Android devices (any that has Google Play installed... so, it won't work on an Amazon Fire phone, for example). It's not "easy" to setup, but they do a lot of really hard stuff for you. Also, there are a lot of references for setting it up, like this from Google: https://developer.android.com/google/gcm/client.html |