从服务器端发送FCM消息到Android设备 [英] Send FCM messages from server side to android device

查看:258
本文介绍了从服务器端发送FCM消息到Android设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我尝试了git中的示例应用程序,它工作得很好。我可以从控制台发送通知。



但是我想在触发某个事件后从服务器发送通知。

  05-20 20:40:58.941 30132-30919 / com我喜欢在GCM中遵循相同的方法,但它不起作用。 .google.firebase.quickstart.fcm E / AndroidRuntime:FATAL EXCEPTION:pool-1-thread-1 
进程:com.google.firebase.quickstart.fcm,PID:30132
java.lang.NullPointerException :尝试在com.google.firebase.quickstart.fcm.MyFirebaseMessagingService的空对象引用
上调用虚拟方法'java.lang.String com.google.firebase.messaging.RemoteMessage $ Notification.getBody()'。 onMessageReceived(MyFirebaseMessagingService.java:53)
,位于com.google.firebase.messaging.FirebaseMessagingService.zzo(未知源)
,位于com.google.firebase.messaging.FirebaseMessagingService.zzn(未知源)
位于com.google.firebase.messaging.FirebaseMessagingService.zzm(未知源)
,位于com.google .firebase.iid.zzb $ 2.run(Unknown Source)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:588)
java.lang.Thread.run(Thread.java:818)
05-20 20:40:5 9.118 30132-30279 / com.google.firebase.quickstart.fcm E / Surface:getSlotFromBufferLocked:unknown buffer:0xb9e83390

遵循这个 PHP脚本发送通知。
如果我尝试执行脚本,我会得到以下结果。

  {multicast_id:4679427854122301046,成功:1,failure:0,canonical_ids:0,results:[{message_id:0:1463757518309261%31bd1c96f9fd7ecd}]} 
pre>

注意:我浏览了他们的 docs 并修改代码的要点是只有主体和标题。即使那样它不起作用。

解决方案

您可以使用这个完整的代码

 <?php 

函数sendFCM($ mess,$ id){
$ url ='https://fcm.googleapis.com/fcm/send';
$ fields = array(
'to'=> $ id,
'notification'=> array(
body=> $ mess,
title=>Title,
icon=>myicon

);
$ fields = json_encode($ fields);
$ headers = array(
'Authorization:key ='。AIzaSyA9vpL9OuX6moOYw-4n3YTSXpoSGQVGnyM,
'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_POSTFIELDS,$ fields);

$ result = curl_exec($ ch);
curl_close($ ch);
}

?>

将消息和标记ID作为参数传递给 sendFCM($ mess, $ id)呼叫。

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.

解决方案

You can use this complete code

<?php

function sendFCM($mess,$id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
        'to' => $id,
        'notification' => array (
                "body" => $mess,
                "title" => "Title",
                "icon" => "myicon"
        )
);
$fields = json_encode ( $fields );
$headers = array (
        'Authorization: key=' . "AIzaSyA9vpL9OuX6moOYw-4n3YTSXpoSGQVGnyM",
        '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_POSTFIELDS, $fields );

$result = curl_exec ( $ch );
curl_close ( $ch );
}

?>

Pass message and token id as a parameter to the sendFCM($mess,$id) call.

这篇关于从服务器端发送FCM消息到Android设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆