Mic*_*rte 5 java android firebase firebase-cloud-messaging
我有一个问题Firebase Notification,如果我在应用程序在屏幕上运行时发送通知,则通知会正确显示,如下所示:
比起当我在后台运行应用程序时发送通知,通知显示如下:
这是我的FirebaseMessagingService课
public class AppFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
sendNotification(remoteMessage);
}
private void sendNotification(RemoteMessage remoteMessage) {
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
int icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.ic_video_label_white_24dp: R.drawable.ic_launcher;
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setWhen(System.currentTimeMillis());
notificationBuilder.setColor(ContextCompat.getColor(this, R.color.colorPrimary));
notificationBuilder.setSmallIcon(icon);
notificationBuilder.setContentTitle(remoteMessage.getNotification().getTitle());
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
这是 Firebase 中的错误,尚未解决。链接在这里:https ://stackoverflow.com/a/37332514/1507602
另一种选择是不使用 Firebase 控制台发送通知,而是使用 POST API,这样您的通知将直接发送到onMessageReceived()您可以创建自己的通知的位置。
要发送数据有效负载消息,您必须发出curl 请求:
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
Run Code Online (Sandbox Code Playgroud)
您可以从 firebase 控制台获取服务器密钥 (AIzaSyZ-1u...0GBYzPu7Udno5aA):您的项目 -> 设置 -> 项目设置 -> 云消息传递 -> 服务器密钥
| 归档时间: |
|
| 查看次数: |
1277 次 |
| 最近记录: |