Wai*_*ein 6 android codeigniter push-notification firebase firebase-cloud-messaging
我正在开发一个使用推送通知功能的Android应用程序.我需要从服务器推送.我使用Firebase.坦率地说,这是我第一次使用Firebase.我是Firebase的新手.但是当我使用PHP和CURL从服务器推送时,它给了我无效的注册错误.
我在Android中获得了这样的Firebase令牌
String token = FirebaseInstanceId.getInstance().getToken();
Run Code Online (Sandbox Code Playgroud)
然后我将该令牌保存到服务器并保存在数据库中.
在服务器上,我这样推
class Pusher extends REST_Controller {
function __construct()
{
parent::__construct();
}
public function notification_get()
{
$rows = $this->db->get('device_registration')->result();
$tokens= array();
if(count($rows)>0)
{
foreach($rows as $row)
{
$tokens[] = $row->token;
}
}
$message = array("message"=>"FCM PUSH NOTIFICATION TESTING");
if(count($tokens)>0)
{
$result = $this->send_notification($tokens,$message);
if(!$result)
{
die("Unable to send");
}
else{
$this->response($result, REST_Controller::HTTP_OK);
}
}
}
function send_notification($tokens,$message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids'=>$tokens,
'data'=>$message
);
$headers = array(
'Authorization:key = AIzaSyApyfgXsNQ3dFTGWR6ns_9pttr694VDe5M',//Server key from firebase
'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));
$result = curl_exec($ch);
if($result==FALSE)
{
return FALSE;
}
curl_close($ch);
return $result;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用CodeIgniter 3框架来构建Rest API.当我从浏览器推送访问URL时,它返回带有错误的JSON数据,如下面的屏幕截图所示.
正如您所看到的,它提供了InvalidRegistration错误,并且消息未被推送到设备.我的代码出了什么问题?
这是我的FirebaseMessagingService类,它在android中显示通知
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
showNotification(remoteMessage.getData().get("message"));
}
private void showNotification(String message)
{
Intent i = new Intent(this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setAutoCancel(true)
.setContentTitle("FCM Test")
.setContentText(message)
.setSmallIcon(R.drawable.info)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}
}
Run Code Online (Sandbox Code Playgroud)
虽然我没有使用 codeigniter,但我InvalidRegistration在发送到iOS设备,所以我想我会在这里分享我的解决方案。
我不得不改变registration_ids到到发送时,在PHP中Notification message的单个设备令牌,并确保价值来是一个字符串,而不是一个数组。
改变这个:
'registration_ids'=>$tokens,
Run Code Online (Sandbox Code Playgroud)
对此:
'to'=>$tokens[0],
Run Code Online (Sandbox Code Playgroud)
无效的注册 ID 检查您传递给服务器的注册 ID 的格式。确保它与手机在 com.google.firebase.INSTANCE_ID_EVENT 意图中收到的注册 ID 相匹配,并且您没有截断它或添加其他字符。当错误代码为 InvalidRegistration 时发生。
请与侧应用侧和您侧确认完全相同的注册 ID 存储在移动应用程序在 ononTokenRefresh方法中接收它的服务器中。您应该收到与开发人员完全相同的注册令牌FirebaseInstanceId.getInstance().getToken()
当我收到您的评论并且您已更新代码时,您的代码中有一些更改,它来自谷歌文档本身...
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
Run Code Online (Sandbox Code Playgroud)
Firebase 具有三种消息类型:
通知消息:通知消息在后台或前台工作。当应用程序在后台时,通知消息会传递到系统托盘。如果应用程序在前台,消息由 onMessageReceived() 或 didReceiveRemoteNotification 回调处理。这些本质上就是所谓的显示消息。
数据消息:在Android平台上,数据消息可以在后台和前台工作。数据消息将由 onMessageReceived() 处理。此处特定于平台的说明是:在 Android 上,可以在用于启动您的活动的 Intent 中检索数据有效负载。
具有通知和数据负载的消息:在后台时,应用程序在通知托盘中接收通知负载,并且仅在用户点击通知时处理数据负载。在前台时,您的应用程序会收到一个消息对象,其中两个有效负载都可用。其次,click_action 参数通常用于通知负载而不是数据负载。如果在数据负载内部使用,此参数将被视为自定义键值对,因此您需要实现自定义逻辑以使其按预期工作。
| 归档时间: |
|
| 查看次数: |
29421 次 |
| 最近记录: |