将GCM消息传递给Fragment - Xamarin

时间:2016-11-11 23:08:42

标签: xamarin

我是Xamarin和Android的新手。我有一个接收gcm通知的应用程序,所以我需要将这些通知传递给包含listview的片段。我想在列表视图中附加这些通知而不重新打开片段 - 就像whatsapp聊天窗口一样。我像这样填写列表视图:

  public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
  List<ChatHistory> lstChatHistory = LocalDB.GetChatHistory(db, communityRefNo);

                for (int x = 0; x < lstChatHistory.Count; x++)
                {
                    _mySimpleItemLoader.LoadMoreItems(lstChatHistory[x].TyperName, lstChatHistory[x].message, lstChatHistory[x].dateCreated);
                    ListViewItemsCount++;
                }

                lvChat.Adapter = new ListViewChatAdapter(this.Context, _mySimpleItemLoader);
}

gcm接收器方法具有以下参数:

public override void OnMessageReceived(string from, Bundle data)
{
    //pass the received info to the listview above without reopening the fragment above
}

1 个答案:

答案 0 :(得分:0)

我通过添加以下代码解决了这个问题:

Gcm服务

id

片段中的接收者

Intent intentReply = new Intent("test");  
                        intentReply.PutExtra("reply", message);
                  Android.Support.V4.Content.LocalBroadcastManager.GetInstance(this).SendBroadcast(intentReply);

MyApp:Fragment

private static MyApp inst; 

[BroadcastReceiver]
[IntentFilter(new[] { "test" })]  
public class HomeBroadcastReciever : BroadcastReceiver
 {
      public override void OnReceive(Context context, Intent intent)
      {
          string replyMessage = intent.GetStringExtra("reply");
           inst.UpdateChat(replyMessage);
      }
 }