php - 使用 PHP 向 Android 应用推送通知
问题描述
我正在尝试使用 PHP 创建推送通知。我已经创建了我的 firebase 帐户,并在我的 android 应用程序中添加了需要添加的内容。我使用 firebase 控制台对其进行了测试,它会收到通知。
现在我正在尝试使用 PHP 创建一个推送通知,下面是我的代码
$apiKey = "apikey"; //Server Key Legacy
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
$token= 'singletoken';
$notification = array('title' =>'test title',
'body' => 'hello message');
$notifdata = array('title' =>'test title data',
'body' => 'hello',
'image' => 'path'
);
$fcmNotification = array (
'to' => $token,
'notification' => $notification,
'data' => $notifdata
);
$headers = array( 'Authorization: key='.$apiKey, 'Content-Type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$fcmUrl);
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($fcmNotification));
$result = curl_exec($ch);
curl_close($ch);
在我的Login Activity
中,我使用下面的代码来获取我的注册 ID
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( this, new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
device_key = instanceIdResult.getToken();
Log.e(TAG, device_key);
}
});
这是我的MyFirebaseMessagingService
课
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onNewToken(String s) {
super.onNewToken(s);
}
@Override
public void onMessageReceived(RemoteMessage message) {
super.onMessageReceived(message);
sendMyNotification(message.getNotification().getBody());
}
private void sendMyNotification(String message) {
Intent intent = new Intent(this, NotificationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "Default";
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My App")
.setContentText(message)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
}
}
我在我的 Android 模拟器上收到通知,但在真实设备上我没有收到任何通知。
解决方案
output
在前面添加错误curl_close
echo curl_error($ch);
推荐阅读
- python - 缩放一个形状以完全包含另一个形状
- sql-server - 在 SQL Server 上使用 DFS-R 而不是镜像
- linux - 是否有包含 java 的 flatpak 运行时?
- python - ValueError:分类指标无法处理多标签指标和连续多输出目标 sklearn 的混合
- javascript - 在 got 中切换网址的最佳方法是什么?
- java - JSoup 解析属性="值"=""
- python-3.x - 使用元组作为字典路径进行排序
- bash - 对花括号如何在 shell 中工作的误解
- python - 单行而不是列上的日期时间
- java - Java层分离模块间的循环依赖