2016-08-31 237 views
0

我想开发一个Xamarin Android应用程序,使用GCM服务发送推送通知。发送GCM推送通知到设备

我遵循Xamarin教程page上提供的教程,教导如何注册GCM,请求设备令牌以及如何创建消息发送方。

所有的工作都很好,除了我想发送一个推送消息到一个特别的设备令牌x,而不是所有的人。

邮件发送者看起来是这样的:

private static void messageSender(string title, string message, string summary) 
    { 
     var jGcmData = new JObject(); 
     var jData = new JObject(); 

     jData.Add("message", message); 
     jData.Add("title", title); 
     jData.Add("summary", summary); 
     jGcmData.Add("to", "/topics/global"); 
     jGcmData.Add("data", jData); 

     var url = new Uri("https://gcm-http.googleapis.com/gcm/send"); 
     try 
     { 
      using (var client = new HttpClient()) 
      { 
       client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json")); 

       client.DefaultRequestHeaders.TryAddWithoutValidation(
        "Authorization", "key=" + API_KEY); 

       Task.WaitAll(client.PostAsync(url, 
        new StringContent(jGcmData.ToString(), Encoding.Default, "application/json")) 
         .ContinueWith(response => 
         { 
          Console.WriteLine(response); 
          Console.WriteLine("Message sent: check the client device notification tray."); 
         })); 
      } 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Unable to send GCM message:"); 
      Console.Error.WriteLine(e.StackTrace); 
     } 
    } 

任何帮助吗?

+0

您确定第二个设备在GCM通道中正确注册了吗? – OrcusZ

回答

1
*client side* 

    private void SendNotification() 
     { 
      var jGcmData = new JObject(); 
      var jData = new JObject(); 

      jData.Add("title", txtTitle.Text); 
      jData.Add("message", txtMessage.Text); 
      jData.Add("summary", txtSummary.Text); 
      jGcmData.Add("to", "/topics/global"); 
      jGcmData.Add("data", jData); 
      var url = new Uri("https://gcm-http.googleapis.com/gcm/send"); 
      try 
      { 
       using (var client = new HttpClient()) 
       { 
        client.DefaultRequestHeaders.Accept.Add(
         new MediaTypeWithQualityHeaderValue("application/json")); 
        client.DefaultRequestHeaders.TryAddWithoutValidation(
         "Authorization", "key=" + API_KEY); 
        Task.WaitAll(client.PostAsync(url, 
          new StringContent(jGcmData.ToString(), Encoding.Default, "application/json")) 
         .ContinueWith(response => 
         { 
          Console.WriteLine(response); 
          Console.WriteLine("Message sent: check the client device notification tray."); 
         })); 
       } 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine("Unable to send GCM message:"); 
       Console.Error.WriteLine(ex.StackTrace); 
      } 
     } 
*phone side* 

public override void OnMessageReceived(string from, Bundle data) 
     { 
      var message = data.GetString("message"); 
      var title = data.GetString("title"); 
      var summary = data.GetString("summary"); 
      Log.Debug("MyGcmListenerService", "From: " + from); 
      Log.Debug("MyGcmListenerService", "Message: " + message); 
      Log.Debug("MyGcmListenerService", "Title: " + title); 
      Log.Debug("MyGcmListenerService", "Summary: " + summary); 
      SendNotification(message, title, summary); 
     } 
     void SendNotification(string message,string title,string summary) 
     { 

      var intent = new Intent(this, typeof(MainActivity)); 
      intent.AddFlags(ActivityFlags.ClearTop); 
      var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot); 
      var notificationBuilder = new Notification.Builder(this) 
       .SetSmallIcon(Resource.Drawable.aalogo2) 
       .SetContentTitle(title) 
       .SetContentText(message) 
       .SetSubText(summary) 
       .SetAutoCancel(true) 
       .SetContentIntent(pendingIntent); 
      var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService); 
      notificationManager.Notify(0, notificationBuilder.Build()); 
     }