如何连接在参考解决方案上触发的事件

问题描述:

ConnectionManager如何获取在AndroidPush类中捕获的expiredRegistrationId?如何连接在参考解决方案上触发的事件

我这样做是错误的吗?

有关如何改进我的解决方案的任何建议?

有没有我可以遵循的模式?

解决方案:经理

public class ConnectionManger 
{ 
    private readonly IPushManager pushManager = new PushManager(); 

    public void NotifyAppUser(List<PushNotificationSubscription> regIds, Alert alert) 
    { 
     pushManager.PushNotification(regIds, alert); 
     var expiredRegistrationId = ?? 

    } 
} 

解决方案:PushNotification

public class PushManager : IPushManager 
    { 

     public void PushNotification(List<PushNotificationSubscription> registeredPhone, Alert alert) 
     { 

      AndroidPush androidPush = new AndroidPush(); 
      androidPush.Push(alert, registeredPhone); 

     }  

    } 


    public class AndroidPush : IPushNotificationStrategy 
    { 

     public void Push(Alert alert, List<string> registrationIds) 
     { 

      // Wait for GCM server 
      #region GCM Events 
      gcmBroker.OnNotificationFailed += (notification, aggregateEx) => 
      { 

       var expiredRegistrationId = aggregateEx.OldId; 
       Q: How do i pass expiredRegistrationId to ConnectionManager class? 
      }; 

     } 



    } 
+0

广告失败的属性到类ConnectionManager,以便它可以由事件处理程序设置。 – jdweng

+0

如果我向ConnectionManager添加失败的属性,AndroidPush无法设置此属性,因为它正由ConnectionManager引用并导致循环引用。 –

+0

您需要使用该类的一个实例。 – jdweng

你有.NET共同游乐场所以有几个选项

假设:

  • 您的解决方案意味着不同的dll's
  • 你需要返回字符串列表(从原来的问题)
  • 你的代码是保持gcmBroker活着所以OnNotificationFailed事件可以触发

那么这应该工作:

在IPushNotificationStrategy界面更改您的签名

List<string> Push(Alert alert, List<string> registrationIds) 

此事件添加到您的IPushManager接口:

event Action<List<string>> ExpiredRegs; 

实施并调用从PushManager此事件:

public event Action<List<string>> ExpiredRegs; 

// call this when Push returns some expiredRegs : 
void OnExpiredRegs(List<string> expiredRegs) => ExpiredRegs?.Invoke(expiredRegs); 

订阅事件ConnectionManger:

pushManager.ExpiredRegs += OnExpiredRegs; 

void OnExpiredRegs(List<string> expiredRegs) 
{ 
    // whatever you need to do 
}