2016-11-11 101 views
0

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

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 
} 

回答

0

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

GCM服务

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

接收在片段

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); 
     } 
} 

MyApp的:片段

public void UpdateChat(string replyMessage) 
{ 
    //update control here 
}