GVKun编程网logo

从客户端发送通知 - Firebase Cloud Messaging - swift 以编程方式(客户端通过http发送怎么写)

3

如果您想了解从客户端发送通知-FirebaseCloudMessaging-swift以编程方式和客户端通过http发送怎么写的知识,那么本篇文章将是您的不二之选。我们将深入剖析从客户端发送通知-Fi

如果您想了解从客户端发送通知 - Firebase Cloud Messaging - swift 以编程方式客户端通过http发送怎么写的知识,那么本篇文章将是您的不二之选。我们将深入剖析从客户端发送通知 - Firebase Cloud Messaging - swift 以编程方式的各个方面,并为您解答客户端通过http发送怎么写的疑在这篇文章中,我们将为您介绍从客户端发送通知 - Firebase Cloud Messaging - swift 以编程方式的相关知识,同时也会详细的解释客户端通过http发送怎么写的运用方法,并给出实际的案例分析,希望能帮助到您!

本文目录一览:

从客户端发送通知 - Firebase Cloud Messaging - swift 以编程方式(客户端通过http发送怎么写)

从客户端发送通知 - Firebase Cloud Messaging - swift 以编程方式(客户端通过http发送怎么写)

如何解决从客户端发送通知 - Firebase Cloud Messaging - swift 以编程方式

是否可以使用 swift 找到来自客户端的推送通知? 我在网上搜索,发现的唯一示例、文档与 Firebase Cloud Functions 的使用和/或 Firebase Console 的使用严格相关。

我想知道是否可以使用来自客户端的 Firebase Cloud Messaging 创建和发送通知。

解决方法

实际上可以使用 firebase rest api,以下端点可用于发送通知:

https://fcm.googleapis.com/fcm/send

HTTP 请求必须是带有以下标头的 POST 请求:

授权:key=

内容类型:应用程序/json

和以下正文:

{
  "to" : "<FCM TOKEN>","collapse_key" : "type_a","priority": 10,"data" : {
    "body" : "Notification","title": "Notification title"
    }
}

当然,您需要知道应该接收通知的目标设备的 FCM 令牌,或者至少知道设备/用户订阅的主题。如果您想向特定主题发送通知,请使用以下正文:

{
  "to" : "/topics/<YOUR_TOPIC>","data" : {
   "body" : "Notification","title": "Notification title"
   }
}

Android Firebase Cloud Messaging(FCM):可以对SubscribeToTopic进行自动重试吗?

Android Firebase Cloud Messaging(FCM):可以对SubscribeToTopic进行自动重试吗?

要在android客户端中订阅主题,我们应该调用:

FirebaseMessaging.getInstance().subscribeToTopic("news");

我想知道如果在执行此指令时互联网连接不可用怎么办?

当互联网连接可用时,Google服务会自动重试订阅吗?还是我们应该由开发人员处理这种情况?

答案1

小编典典

更新:

subscribeToTopic()现在返回一个,Task<Void>因此您可以附加一个,OnCompleteListener()以检查它是否成功。

更新:

因此,根据@DiegoGiorgini在您以前的文章中的评论,似乎还有更多的subscribeToTopic:

subscribeToTopic会在后台继续重试,但它与您的应用生命周期(尚未与Google服务相关)相关。因此,如果您的应用程序被杀死,它将停止重试,直到再次打开该应用程序为止。(操作已存储,因此将在再次启动应用程序时恢复操作)

我之前尝试过的方式是在给定时间段之后,我将终止该应用程序,而没有再次将其启动。

所以我尝试检查一下。看起来,如果在发送订阅请求时设备处于脱机状态,它将重试一段时间(20-30秒左右?),然后如果仍未连接,它将停止。该请求可能达到了超时错误。但是,由于没有SubscribeToTopic()的返回值,因此您无法通过客户端应用程序确定此返回值。您可以根据需要发送功能请求。

但是,到目前为止,我认为您可以做的一种方法是在客户端应用程序中安装一个检查器,如果只有设备在线,您将在其中发送请求。

您还可以通过你的应用服务器检查它,因为我在你所提到以前的职位。

Android 设备未收到 Firebase Cloud Messaging 推送通知

Android 设备未收到 Firebase Cloud Messaging 推送通知

如何解决Android 设备未收到 Firebase Cloud Messaging 推送通知

我正在尝试解决当推送通知从 FCM 发送到 Android 设备时设备上未收到通知的问题。我已经在 Android Studio 上尝试了实际设备和模拟器。

来自我的 index.js 文件的代码:

exports.onTrxnupdate = functions.firestore.document(''/trxns/{trxnId}'').onUpdate(async (change,context) => {
    
    const afterData = change.after.data();
    const agentId = afterData.agentId;
    /*console.log(''context: '',context);*/
    console.log(''A transaction has been updated'');
    
    /**** GET DEVICE informatION ****/
    const deviceDoc = db.collection(''device'').doc(agentId);
    /*console.log(''deviceDoc: '',deviceDoc);*/
    if (deviceDoc == null) {
        console.log(''No device document found'');
    } 
    
    const deviceData = await deviceDoc.get(); 
    const deviceid = deviceData.get(''token'');
    /*console.log(''deviceid: '',deviceid);*/
    
    if (deviceid == null) {
        return console.error(''No device tokens found'');
    }

    /**** GET TRXN informatION ****/
    const trxnId = context.params.trxnId;
    const trxnDoc = db.collection(''trxns'').doc(trxnId);
    if (trxnDoc == null) {
        console.log(''No trxn document found'');
    } 
    const trxnData = await trxnDoc.get();  
    const clientFName = trxnData.get(''clientFName'');
    const clientLName = trxnData.get(''clientLName'');
    console.log(''Trxn: '' + clientFName + '' '' + clientLName + '' updated '');

    let title = "Transaction updated";
    let body = "Trxn: " + clientFName + " " + clientLName + " updated ";

    const payload = {

        notification: { title: title,body: body},data: {click_action: ''FlutteR_NOTIFICATION_CLICK'' }

    };

    const response = await admin.messaging().sendToDevice(deviceid,payload);

    if (response.error) {

        console.error("Error sending message: ",response.error);

    } else {

        return console.log("Message sent successfully!");

    };

    return Promise.all(console.log(''End of function'')); 

});

云消息日志输出:

6:34:40.391 AM
onTrxnupdate
Function execution started
6:34:40.840 AM
onTrxnupdate
A transaction has been updated
6:34:41.227 AM
onTrxnupdate
Trxn: Erica Kane updated
6:34:41.366 AM
onTrxnupdate
Message sent successfully!
6:34:41.370 AM
onTrxnupdate
Function execution took 981 ms,finished with status: ''ok''

代码中似乎一切正常,但通知从未到达设备。

android-FIrebase Cloud Messaging,无法解析依赖项com.google.firebase:firebase-core:9.0.2

android-FIrebase Cloud Messaging,无法解析依赖项com.google.firebase:firebase-core:9.0.2

我试图按照here给出的指南使用Firebase Cloud Messaging编写应用程序,但我为添加实现FCM的依赖项而感到惊讶(com.google.firebase:firebase-core:9.0.2).每次我尝试通过项目结构添加它时,它都不会在搜索结果中列出依赖项.当我尝试将其添加到应用程序级别gradle中时,它给我一个错误,导致无法解决依赖关系.我不知道我要去哪里错了,但是请帮我解决这个问题.

解决方法:

从您的SDK管理器下载更新.我遇到过同样的问题.从SDK经理更新Google Play服务和Google Repository对我来说很有效.

Azure Messaging-ServiceBus Messaging消息队列技术系列4-复杂对象消息是否需要支持序列化和消息持久化 Azure Messaging-ServiceBus Messaging消息队列技术系列3-消息顺序保证

Azure Messaging-ServiceBus Messaging消息队列技术系列4-复杂对象消息是否需要支持序列化和消息持久化 Azure Messaging-ServiceBus Messaging消息队列技术系列3-消息顺序保证

在上一篇中,我们介绍了消息的顺序收发保证:

Azure Messaging-ServiceBus Messaging消息队列技术系列3-消息顺序保证

在本文中我们主要介绍下复杂对象消息是否需要支持序列化以及消息的持久化。

在实际的业务应用开发中,我们经常会将复杂业务对象放到消息里面,实现异构系统之间的集成、模块间的解耦等等。

同时,我们还比较关注消息队列服务是否支持消息的持久化,消息队列如果宕机后持久化的消息是否可以还原?

在Azure Messaging的官方说明中,没有特地的介绍复杂对象消息是否需要支持序列化的要求,但是,我们在上篇博文中,有个消息创建方法,as following,

brokeredMessage类的构造函数:

//
        // Summary:
             Constructor that creates a brokeredMessage from a given object using the
             provided XmlObjectSerializer
         Parameters:
           serializableObject:
             The serializable object.
           serializer:
             The serializer object.
         Exceptions:
           System.ArgumentNullException:
             Thrown when null serializer is passed to the method with a non-null serializableObject
         Remarks:
             You should be aware of the exceptions that their provided Serializer can
             throw and take appropriate actions. Please refer to for a possible list of
             exceptions and their cause.
        public brokeredMessage(object serializableObject,XmlObjectSerializer serializer);

看来消息的构造,支持动态传入XmlObjectSerializer,so,

 1         /// <summary>
 2         /// 构造消息
 3         </summary>
 4         <param name="serializableObject">可序列化的对象</param>
 5         <returns>消息</returns>
 6         public brokeredMessage Create(Object serializableObject)
 7         {
 8             var serializer = new DataContractSerializer(serializableObject.GetType(), 9                 new DataContractSerializerSettings() { IgnoreExtensionDataObject = true,PreserveObjectReferences = true });
10             var message =  brokeredMessage(serializableObject,serializer);
11             message.Properties.Add("Type",serializableObject.GetType().ToString());
12 
13             return message;
14         }

接下来,我们用上一篇中的代码,做一个复杂对象消息收发的测试,我们还是用上次的SalesOrder类,但是增加一个SalesOrderItem集合和双向关联,来描述销售订单和销售订单明细的的1:n的业务领域模型。

using System;
 System.Collections.Generic;
 System.Linq;
 System.Text;
 System.Threading.Tasks;

namespace AzureMessaging.FIFO
{
    <summary>
     销售订单类
    </summary>
    public class SalesOrder
    {
        <summary>
         订单ID
        </summary>
        string OrderID { get; set; }

         订单编号
        string Code {  创建时间
        public DateTime CreateTime {  总价格
        public Decimal TotalPrice {  产品ID
        int ProductID { ; }

        private List<SalesOrderItem> items;

         销售订单明细
        public List<SalesOrderItem> Items
        {
            get
            {
                if (items == null)
                    items = new List<SalesOrderItem>();

                 items;
            }
            
            {
                items = value;
            }
        }
    }
}
 销售订单明细
     SalesOrderItem
    {
         标识
        string ID {  客户ID
        int CustomerID {  所属的销售订单ID
        string SalesOrderID
        {
            if (Order != )
                     Order.OrderID;

                return .Empty;
            }
        }

         所属的销售订单
        public SalesOrder Order { ; }
    }
}

创建销售订单实例类方法:

  private static SalesOrder CreateSalesOrder(int i)
        {
            var order = new SalesOrder() { OrderID = i.ToString(),Code = SalesOrder_" + i,CreateTime = DateTime.Now,ProductID = 17967,TotalPrice = new decimal(19999) };
            order.Items.Add(new SalesOrderItem() { ID = Guid.NewGuid().ToString(),Order = order,CustomerID = 1234567 });

             order;
        }

在构造SalesOrder和SalesOrderItems时,我们做了双向关联。
消息顺序收发测试:

 1  Microsoft.ServiceBus.Messaging;
 2  3  4  5  6  System.Threading.Tasks;
 7 
 8  AzureMessaging.FIFO
 9 {
10      Program
11     {
12         static readonly string queueName = OrderQueue;
13         void Main([] args)
14 15             MessageSend();
16             Console.ReadKey();
17 
18             MessageReceive();
19 20         }
21 
22         23          发送消息
24         25         void MessageSend()
26 27             var sbUtils =  ServiceBusUtils();
28 
29             创建队列
30             sbUtils.CreateQueue(queueName,false);
31 
32             顺序发送消息到OrderQueue
33             var queueSendClient = sbUtils.GetQueueClient(queueName);
34             for (int i = 0; i < 10; i++)
35             {
36                 var order = CreateSalesOrder(i);
37                 var message = sbUtils.Create(order);
38                 queueSendClient.Send(message);
39                 Console.WriteLine(string.Format(Send {0} MessageID: {1}40             }
41 
42             Console.WriteLine(Send Completed!43 44 
45         46          接收消息
47         48          MessageReceive()
49 50             int index = 051             brokeredMessage msg = 52             53             var queueReveiveClient = sbUtils.GetReceiveQueueClient(queueName,ReceiveMode.ReceiveAndDelete);
54             while ((msg = queueReveiveClient.Receive(TimeSpan.FromMilliseconds(3))) != 55 56                 Console.WriteLine(Received {0} MessageID: {1}57                 index++58 59 
60             /删除队列
61             sbUtils.DeleteQueue(queueName);
62 
63             Console.WriteLine(Receive Completed!64 65 
66          i)
67 68             ) };
69             order.Items.Add(70 
71              order;
72 73     }
74 }

 

可以看出,复杂对象消息只要指定适当的XmlObjectSerializer,即可。

在双向引用这种领域模型的设计场景下,我们配置了PreserveObjectReferences = true

true });

解决了序列化时循环引用的问题。
关于消息的持久化,Azure messaging有官方的说明:所有的队列都是持久化的,持久化存储是sql Server,不提供内存中的消息队列。

毕竟是PaaS层的消息队列服务,消息的持久化和高可用性微软还是有保障的。

 

本篇中我们介绍并验证了Azure Messaging Service Bus复杂对象消息是否需要支持序列化和消息持久化,下一篇我们继续介绍消息的重复发送问题。

 

周国庆

2017/3

 

总结

以上是小编为你收集整理的Azure Messaging-ServiceBus Messaging消息队列技术系列4-复杂对象消息是否需要支持序列化和消息持久化 Azure Messaging-ServiceBus Messaging消息队列技术系列3-消息顺序保证全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

关于从客户端发送通知 - Firebase Cloud Messaging - swift 以编程方式客户端通过http发送怎么写的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Android Firebase Cloud Messaging(FCM):可以对SubscribeToTopic进行自动重试吗?、Android 设备未收到 Firebase Cloud Messaging 推送通知、android-FIrebase Cloud Messaging,无法解析依赖项com.google.firebase:firebase-core:9.0.2、Azure Messaging-ServiceBus Messaging消息队列技术系列4-复杂对象消息是否需要支持序列化和消息持久化 Azure Messaging-ServiceBus Messaging消息队列技术系列3-消息顺序保证等相关内容,可以在本站寻找。

本文标签: