Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 如何使用FCM和颤振(一个设备到另一个设备)发送推送通知?_Firebase_Flutter_Firebase Cloud Messaging - Fatal编程技术网

Firebase 如何使用FCM和颤振(一个设备到另一个设备)发送推送通知?

Firebase 如何使用FCM和颤振(一个设备到另一个设备)发送推送通知?,firebase,flutter,firebase-cloud-messaging,Firebase,Flutter,Firebase Cloud Messaging,我已经在Flatter中开发了一对一聊天系统,希望使用FCM向另一个设备发送推送通知 我已经设置了所有的颤振和firebase消息传递要求 //In InitState() _firebaseMessaging.onTokenRefresh.listen(sendTokenToServer); _firebaseMessaging.getToken(); _firebaseMessaging.configure(onLaunch: (Map<String, dynamic&

我已经在Flatter中开发了一对一聊天系统,希望使用FCM向另一个设备发送推送通知

我已经设置了所有的颤振和firebase消息传递要求

//In InitState()
_firebaseMessaging.onTokenRefresh.listen(sendTokenToServer);
    _firebaseMessaging.getToken();
    _firebaseMessaging.configure(onLaunch: (Map<String, dynamic> msg) {
      print("onLaunch");
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => Message(this.user, this.event)),
      );
    }, onResume: (Map<String, dynamic> msg) {
      print("onResume");
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => Message(this.user, this.event)),
      );
    }, onMessage: (Map<String, dynamic> msg) {
      print("onMessage");
    });
    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(sound: true, alert: true, badge: true));
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings setting) {
      print("IOS");
    });

//sendTokenToServer() - function send FCM token my Postgres DB

//When user clicks on send Button

Future sendNotification(userData, eventData) async {
    await Messaging.sendToAll(
      title:
          "${toBeginningOfSentenceCase(userData['User']['name'])} on ${toBeginningOfSentenceCase(eventData['Event']['eventName'])} event",
      body: _messageController.text,
      fcmToken: fcmTokenToServer,
    );
  }

//Messaging.sendToAll()
static Future<Response> sendToAll(
          {@required String title,
          @required String body,
          @required String fcmToken}) =>
      sendTo(title: title, body: body, fcmToken: fcmToken);

  static Future<Response> sendTo({
    @required String title,
    @required String body,
    @required String fcmToken,
  }) =>
      client.post(
        'https://fcm.googleapis.com/fcm/send',
        body: json.encode({
          'notification': {'body': '$body', 'title': '$title'},
          'priority': 'high',
          'data': {
            'click_action': 'FLUTTER_NOTIFICATION_CLICK',
            'id': '1',
            'status': 'done',
          },
          'to': '$fcmToken',
        }),
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'key=$serverKey',
        },
      );
//在InitState()中
_firebaseMessaging.onTokenRefresh.listen(sendTokenToServer);
_firebaseMessaging.getToken();
_firebaseMessaging.configure(onLaunch:(映射消息){
打印(“onLaunch”);
导航器。推(
上下文
MaterialPage路由(生成器:(上下文)=>消息(this.user,this.event)),
);
},onResume:(映射消息){
打印(“简历”);
导航器。推(
上下文
MaterialPage路由(生成器:(上下文)=>消息(this.user,this.event)),
);
},onMessage:(映射消息){
打印(“onMessage”);
});
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(声音:真、警报:真、徽章:真);
_firebaseMessaging.onissettings已注册
.listen((IONotificationSettings设置){
打印(“IOS”);
});
//sendTokenToServer()-函数send FCM token my Postgres DB
//当用户单击发送按钮时
未来发送通知(userData、eventData)异步{
等待消息。发送到所有(
标题:
“${toBeginningOfSentenceCase(事件数据['Event']['eventName'])事件上的${toBeginningOfSentenceCase(userData['User']['name'])}事件”,
正文:_messageController.text,
fcmToken:fcmTokenToServer,
);
}
//Messaging.sendToAll()
静态未来发送到所有(
{@必需的字符串标题,
@所需的字符串体,
@必需的字符串fcmToken})=>
发送到(标题:标题,正文:正文,fcmToken:fcmToken);
静态未来发送到({
@必需的字符串标题,
@所需的字符串体,
@必需字符串fcmToken,
}) =>
client.post(
'https://fcm.googleapis.com/fcm/send',
正文:json.encode({
'通知':{'body':'$body','title':'$title'},
“优先级”:“高”,
“数据”:{
“点击动作”:“颤振通知点击”,
“id”:“1”,
“状态”:“完成”,
},
'收件人':'$fcmToken',
}),
标题:{
“内容类型”:“应用程序/json”,
“授权”:“key=$serverKey”,
},
);

但没有收到推送通知。是否我必须实现云功能才能发送通知???

我可以在没有服务器的情况下使用topic/FCM令牌将FCM消息从一个设备发送到另一个设备

注意:在客户端使用服务器密钥是一种不好的做法,不应在生产级应用程序中使用

static Future sendfcmessage(字符串标题、字符串消息)异步{
试一试{
var url='1〕https://fcm.googleapis.com/fcm/send';
变量头={
“内容类型”:“应用程序/json”,
“授权”:
“密钥=您的服务器密钥”,
};
var请求={
“通知”:{
“头衔”:头衔,
“文本”:消息,
“声音”:“默认值”,
“颜色”:“990000”,
},
“优先级”:“高”,
“收件人”:“/主题/全部”,
};
var client=new client();
var响应=
wait client.post(url,headers:header,body:json.encode(请求));
返回true;
}渔获物(东、南){
印刷品(e);
返回false;
}
}
如果必须使用FCM令牌发送数据请求,请使用

request = {
      'notification': {'title': title, 'body': message},
      'data': {
        'click_action': 'FLUTTER_NOTIFICATION_CLICK',
        'type': 'COMMENT'
      },
      'to': 'fcmtoken'
    };

我可以在没有服务器的情况下使用topic/FCM令牌从一个设备向另一个设备发送FCM消息

注意:在客户端使用服务器密钥是一种不好的做法,不应在生产级应用程序中使用

static Future sendfcmessage(字符串标题、字符串消息)异步{
试一试{
var url='1〕https://fcm.googleapis.com/fcm/send';
变量头={
“内容类型”:“应用程序/json”,
“授权”:
“密钥=您的服务器密钥”,
};
var请求={
“通知”:{
“头衔”:头衔,
“文本”:消息,
“声音”:“默认值”,
“颜色”:“990000”,
},
“优先级”:“高”,
“收件人”:“/主题/全部”,
};
var client=new client();
var响应=
wait client.post(url,headers:header,body:json.encode(请求));
返回true;
}渔获物(东、南){
印刷品(e);
返回false;
}
}
如果必须使用FCM令牌发送数据请求,请使用

request = {
      'notification': {'title': title, 'body': message},
      'data': {
        'click_action': 'FLUTTER_NOTIFICATION_CLICK',
        'type': 'COMMENT'
      },
      'to': 'fcmtoken'
    };

我可以使用FCM令牌和主题id将通知从一个设备发送到另一个设备。代码也一样。请尝试与主题以及可能的令牌是wrong@shyju-madathil您是否使用firestore将令牌或您的db存储在服务器上??没有服务器…一个设备到另一个设备。在我的应用程序中,要向post OwnerId发送评论通知,您可以解决吗?我可以使用FCM令牌以及主题id将通知从一个设备发送到另一个设备。代码也相同。请尝试与主题以及可能的令牌是wrong@shyju-madathil您是否使用firestore将令牌或您的db存储在服务器上??没有服务器…一个设备到另一个设备。在我的应用程序中,要向post OwnerId发送评论通知,您可以解决它吗?如何将此添加到将发送到特定设备的主题
/topics/user-{userId}
?您只能使用fcmtoken发送,不能使用带有useridfirebaser的主题在客户端代码中使用
“key=your_server_key”
,这是一个严重的安全风险,因为它允许恶意用户向您的用户发送他们想要的任何消息。这是一种不好的做法,不应在生产级应用程序中使用。要获得更好的方法,请参阅和我的回答:如何将此添加到将发送到特定设备的主题
/topics/user-{userId}
?只能使用fcmtoken发送,不能在此处使用带有useridfirebaser的主题使用
“key=your_server_key”