2016-11-11 98 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 
}