2

使用FCM,当应用程序处于后台或未运行时,系统托盘中会收到推送通知。当应用程序在前台时,我可以覆盖onMessageReceived并使用NotificationCompat创建我自己的单挑通知。Firebase消息传递 - 在后台应用程序中创建抬头显示

有没有办法在我的应用程序处于后台或未运行时创建抬头通知?

感谢

编辑:仅供参考这里的消息有效载荷https://fcm.googleapis.com/fcm/send

{ 
    "to":"push-token", 
    "content_available": true, 
    "priority": "high", 
    "notification": { 
     "title": "Test", 
     "body": "Mary sent you a message!", 
     "sound": "default" 
    }, 
    "data": { 
     "message": "Mary sent you a Message!", 
     "notificationKey":"userID/notification_type", 
     "priority": "high", 
     "sound": "default" 
    } 
} 

回答

1

我通过卷曲仅使用,如果你使用的是一些其他的应用程序时,您会得到抬起头来通知你应用程序在后台或未运行。如果您的手机未被使用,您将收到系统托盘通知或锁定屏幕通知。

如果您正在使用应用服务器通过http协议发送推送通知,那么您甚至可以在发送到fcm端点的json数据中将优先级设置为高。

如果您使用Firebase控制台,则在高级通知部分设置下确保优先级高。

高优先级将确保您在大多数情况下收到抬头通知。

编辑:这是你编辑的JSON看起来应该像成功的测试 -

{ 
    "to":"push-token", 
    "priority": "high", 
    "notification": { 
     "title": "Test", 
     "body": "Mary sent you a message!", 
     "sound": "default", 
     "icon": "youriconname" 
    } 
} 

youriconname是要设置为您的通知图标绘制资源的名称。

我为测试目的省略了数据。正是这么多,应该让你领导通知。

+1

感谢您的回复。我通过curl将我使用的JSON有效载荷添加到了我的问题中。我只是加倍检查,当我在另一个应用程序(或应用程序未运行)时收到声音通知,但我根本没有收到单独通知。 –

+0

看着你的json。我会立即建议你两件事 - 从数据**中删除**声音和优先级。只保存在一个位置。另外**除非您为iOS开发**,否则不需要设置content_available。一旦使用此设置进行测试,并在完全移除数据的情况下进行测试,则只会按照建议保留通知,然后查看其行为。 –

+0

另请注意,您不必在使用fcm时使用通知兼容构建器进行通知。由于fcm会自己处理它。我现在还要求您删除任何此类通知代码。一旦你从纯粹的fcm得到正确的头像通知,那么你可以进一步自定义。** –

3

我找到了解决办法: 我只是从我们的本地服务器,然后我生成MyFirebaseMessagingService回调发送到服务器火力JSON删除通知标记:onMessageReceived()方法。 n此方法我使用NotificationCompat.Builder类生成本地通知。这里是Android的代码:

private void sendNotification(RemoteMessage remoteMessage) { 
     Intent intent = new Intent(this, SplashActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     intent.putExtra(AppConstant.PUSH_CATEGORY, remoteMessage.getData().get("category")); 
     intent.putExtra(AppConstant.PUSH_METADATA, remoteMessage.getData().get("metaData")); 
     intent.putExtra(AppConstant.PUSH_ACTIVITY, remoteMessage.getData().get("activity")); 
     intent.putExtra(AppConstant.PUSH_ID_KEY, remoteMessage.getData().get("_id")); 

     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.mipmap.ic_launcher) 
       .setContentTitle(remoteMessage.getData().get("title")) 
       .setContentText(remoteMessage.getData().get("body")) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setPriority(NotificationCompat.PRIORITY_HIGH) 
       .setContentIntent(pendingIntent); 

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

     notificationManager.notify(0, notificationBuilder.build()); 
    } 
相关问题