【发布时间】:2020-01-07 11:18:46
【问题描述】:
最近我从 GCM 迁移到 FCM,过去几天我一直在努力让它工作。
Android 应用正在接收来自 google firebase 控制台的通知,但它们没有来自 php 服务器。
这是我的 PHP 服务器端代码:
<?php
define("GOOGLE_API_KEY", Setting::get('browser_key') ? Setting::get('browser_key') : "");
class GCM {
function __construct() {
}
public function send_notification($registatoin_ids, $message) {
Log::info("GOOGLE_API_KEY".GOOGLE_API_KEY);
include_once 'const.php';
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
Log::info('***** PUSH MESSAGE ******'.print_r(json_encode($fields),true));
$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_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
Log::info(print_r($result,true));
if ($result === FALSE) {
//die('Curl failed: ' . curl_error($ch));
Log::error('Curl failed: ' . curl_error($ch));
}
else{
//echo $result;
Log::error($result);
}
// Close connection
/*curl_close($ch);
echo $result/*."\n\n".json_encode($fields); */
}
}
?>
这是我的 const.php
<?php
define('TEAM','team');
define('MESSAGE' , 'message');
?>
这是我的 firebase 消息传递代码:
public class MessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Message";
public MessagingService() {
super();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
sendNotification(remoteMessage.getNotification().getBody());
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification());
}
private void sendNotification(String body) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent= PendingIntent.getActivity(this, 0,intent,PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle("Codegama");
notificationBuilder.setContentText(body);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
}
}
这是我在 logcat android studio 中不断遇到的错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on a null object reference
at hr.trazim.client.services.MessagingService.onMessageReceived(MessagingService.java:32)
at com.google.firebase.messaging.FirebaseMessagingService.zzd(Unknown Source:60)
at com.google.firebase.iid.zzg.run(Unknown Source:4)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at com.google.android.gms.common.util.concurrent.zza.run(Unknown Source:6)
at java.lang.Thread.run(Thread.java:919)
2020-01-07 12:08:11.092 1732-29214/? E/ResolverController: 没有有效的 NAT64 前缀 (101, /0)
我阅读了很多关于此类 stackoverflow 主题的内容,但找不到合适的修复/解决方案。我希望这个修复能够帮助像我这样的其他开发者找到合适的答案,而不会像我一样浪费太多时间。
附:如果您需要我的令牌,我还没有迁移它,但它在这里工作的是代码:
public class InstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.e("FCMToken", refreshedToken);
saveDeviceToken(refreshedToken);
}
private void saveDeviceToken(String token) {
UserDataSource.getDataInstance().putDeviceToken(token);
}
}
提前致谢。
【问题讨论】:
-
stackoverflow.com/a/40256179/7229971 参考这个链接,我认为在android中你不需要改变。
-
昨天我尝试了这篇文章中提供的所有解决方案,不幸的是我找不到修复方法。我已经为您的第一个链接提供了 sid,我现在正在检查第二个。
-
你在 android 中遇到错误,对吗?后端工作正常吗?
-
后端发送数据,但android无法处理。说空