2016-12-26 73 views

回答

0

发表的帖子调用https://fcm.googleapis.com/fcm/send具有以下参数: -

头: -

内容类型 - 应用程序/ JSON

Authorization-- key = {你的服务器密钥}

正文: -

{ 
    "data": { 
     "my_custom_key" : "my_custom_value", 

     "message" : "notification message" 
    }, 
    "registration_ids": ["device_token1,device_token2,.........."] 
} 

编辑: -

基本上你需要做的是,每当ü需要发送通知,你必须从你的服务器端和您的应用程序调用此POST方法将自动获得在OnMessageReceived通话。你可以这样处理: -

@Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     super.onMessageReceived(remoteMessage); 
     // TODO: Handle FCM messages here. 
     // If the application is in the foreground handle both data and notification messages here. 
     // Also if you intend on generating your own notifications as a result of a received FCM 
     // message, here is where that should be initiated. 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 

     Map<String, String> data=remoteMessage.getData(); 
     Log.d(TAG, "From: " + data.toString()); 
     String value=data.get("my_custom_key"); 
     Log.d(TAG, "From: " + value); 
     String msg=data.get("message"); 
     Log.d(TAG, "From: " + msg); 

     sendNotification(msg,value,remoteMessage.getSentTime()); 
    } 
+0

'“registration_ids”:[“device_token1,device_token2,..........”]:我想发送给安装该应用程序的任何用户。参考更新的问题。从使用用户段选项的Firebase控制台,我可以发送而不需要设备令牌。由于我的通知是用户安装应用程序时的通用 –

+0

,因此会生成刷新令牌。将此令牌发送到您的服务器,这是设备令牌 – Nainal

+0

,您可以将令牌保存在您的服务器 – Nainal

-1

你将不得不记住一些事情。 首先,你必须附上标题:

Content-Type--application/json 

Authorization--key={your server key with key= in the value.Don't miss key= here} 

接下来的事情是身体:

{ 
    "data": { 
      "my_custom_key" : "chat", 
      "message" : "chat msg", 
      "priority":"high" 

     }, 

     "registration_ids": ["ids of the users"] 
    } 

这是你request.Their的身体有两种类型:数据和notification.You可以使用他们中的任何一个或两者。你可以理解这种差异,从这个link。基本上,如果您发送数据,您将不得不在前台和后台阶段处理通知。在通知类型中,后台通知将通过通知托盘进行处理。您可以查看链接。

然后你有一个json字符串数组,你将不得不发送所有你想发送通知的用户标记。你可以添加更多的功能,你可以在firebase文档中看到这是基本的实现为你。希望这可以帮助

+0

''registration_ids“ :[“用户的ids”]':我如何发送给安装应用程序的用户段而不是id。参考更新的问题。如何使用服务器端实现与图像中显示的相同的东西url –

+0

检查这个http://stackoverflow.com/questions/37995870/how-to-send-firebase-notifications-to-audience-via-http.I不要不认为他们是一种方式。 –

+0

所以没办法做到这一点 –

1

不幸的是,它是not possible to send messages to User Segments using the FCM REST API

作为一种替代方案,您必须使用send messages to multiple devices的其他方式,如使用registration_ids参数和主题消息(我认为这是您的最佳用例)。

以下是关于如何发送此using PostmancURL的示例。

+0

我不能要求我的会员订阅主题。谁安装应用程序将开始得到通知 - –

+0

我知道了@SanthoshYedidi - 你应该继续前进,并使用'registration_ids'。如果你有超过一千个用户,你将不得不提出批量请求,每个请求最多有1000个注册令牌。 –

0

订阅用户根据不同的操作系统

话题上: “机器人” 为Android用户

话题: “iOS版” 适用于iOS用户

(或任何你想要的名称)

然后发送到该主题...

1

我找到了一个解决方案 你可以订阅你的应用程序到一个特定的主题,例如你在FirebaseInstanceIdService R级的应用程序包名称,这样你们可以发送数据按摩像

{ 
    "to" : "/topics/your_package_name", 
    "data" : { 
    "key1" : "value 1", 
    "key2": "value 2", 
    ... 
    } 
} 

这里是订阅您的应用程序,以主题FirebaseInstanceIdService类

 public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService 
    { 
     private final String TAG="your_tag"; 
     @Override 
     public void onTokenRefresh() { 


     // Get updated InstanceID token. 
     String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
     FirebaseMessaging.getInstance().subscribeToTopic("your_app_package_name"); 
     } 
} 

它的工作对我来说

+0

我可以获得数据[按摩](https://en.wikipedia.org/wiki/Massage)吗?听起来不错。 – 0xbaadf00d

+0

是的,我认为是。这是非常好的和棘手的解决方案 –

相关问题