Send FCM messages from server side to android devi

2019-01-11 14:00发布

With the new update, FCM is now going to be used.

I tried the sample app from git and it's working all fine. I can send notifications from the console.

But I want to send the notification from the server after a certain event is triggered. I followed the same approach like in GCM but it's not working.

05-20 20:40:58.941 30132-30919/com.google.firebase.quickstart.fcm E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
                                                                                    Process: com.google.firebase.quickstart.fcm, PID: 30132
                                                                                    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on a null object reference
                                                                                        at com.google.firebase.quickstart.fcm.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:53)
                                                                                        at com.google.firebase.messaging.FirebaseMessagingService.zzo(Unknown Source)
                                                                                        at com.google.firebase.messaging.FirebaseMessagingService.zzn(Unknown Source)
                                                                                        at com.google.firebase.messaging.FirebaseMessagingService.zzm(Unknown Source)
                                                                                        at com.google.firebase.iid.zzb$2.run(Unknown Source)
                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                                        at java.lang.Thread.run(Thread.java:818)
05-20 20:40:59.118 30132-30279/com.google.firebase.quickstart.fcm E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb9e83390

Am following this PHP Script to send the notification. If I try to execute the script, I get the following result.

{"multicast_id":4679427854122301046,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1463757518309261%31bd1c96f9fd7ecd"}]}

NOTE : I went through their docs and modified the code is gist to have only body and title. Even then it's not working.

7条回答
冷血范
2楼-- · 2019-01-11 14:42

function send_fcm($tokens,$message)
          {

	    $url = 'https://fcm.googleapis.com/fcm/send';
	    $priority="high";
	  
	
	    $fields = array(
	         'registration_ids' =>$tokens,         
	          'data' =>$message
	           
	        );

	 
		   $headers = array(
		        'Authorization:key=your key',
		        'Content-Type: application/json'
		        );

		   $ch = curl_init();
		   curl_setopt($ch, CURLOPT_URL, $url);
		   curl_setopt($ch, CURLOPT_POST, true);
		   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
		   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		   curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
		   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
		 echo  json_encode($fields);
		   $result = curl_exec($ch);           
		   curl_error($ch);
		   if ($result === FALSE)
		    {
		       die('Curl failed: ' . curl_error($ch));
   		    }
                  curl_close($ch);
                  return $result;
             }

store device ids in token variable in an array format

查看更多
登录 后发表回答