gpt4 book ai didi

android - 如何使用 Firebase 将推送通知从服务器发送到应用程序?

转载 作者:搜寻专家 更新时间:2023-11-01 08:33:11 25 4
gpt4 key购买 nike

我正在尝试使用 Firebase 从服务器向应用程序发送推送通知,但是当通知到达应用程序时,我的应用程序崩溃了。

PHP代码:

       <?php

$registrationIds = array("USER-DEVICE-TOKEN" );

$message = array
(
'message' => 'My awesome message',
'title' => 'My awesome title',
'subtitle' => 'My awesome subtitle',
'tickerText' => 'My awesome Ticker text',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);


$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $registrationIds,
'data' => $message
);
$headers = array(
'Authorization:key = GOOGLE-API-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));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);

echo $result;

Output of PHP Script

我的 Firebase 消息服务:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); // LINE 24
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getNotification().getTitle());
}

//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(String messageBody,String messageTitle) {
Intent intent = new Intent(this, LoginActivity.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)
.setSmallIcon(R.drawable.mainlogo)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0, notificationBuilder.build());
}
}

Crashing error when push notification comes

希望得到您的帮助

最佳答案

检查 remoteMessage.getNotification() 是否不为 NULL。我认为它是 NULL,因为您没有在 PHP 代码的 $fields 数组中发送参数“通知”。您正在发送“数据”。因此,当您发送参数“数据”时,您必须使用 remoteMessage.getData() 获取该参数。

尝试在 onMessageReceived 方法中打印 remoteMessage.getData().toString() 的内容。它应该打印您在通知中发送的数据。

希望这对您有所帮助。

关于android - 如何使用 Firebase 将推送通知从服务器发送到应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39035990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com