2017-10-21 85 views
1

Je veux ajouter une action sur un Bouton d'une notification (Archive) comme lorsque quelqu'un clique dessus, il affiche un message de pain grillé, au-dessous est un exemple d'image:Bouton de notification Android (AddAction) pour faire un message Toast lorsqu'il est pressé

Image Ceci est mon PendingIntent ligne de code:

var contentIntent = PendingIntent.GetActivity(context, 0, resultIntent, PendingIntentFlags.CancelCurrent); 

Ceci est ma notification Bu Code ilder:

var builder = new NotificationCompat.Builder(context) 
      .SetContentIntent(contentIntent).SetSmallIcon(Resource.Drawable.ic_launcher) 
                .SetContentTitle(title) 
      .SetStyle(style).SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis()) 
                .AddAction(Resource.Drawable.tick_notify, "ARCHIVE", contentIntent) 
      .AddAction(Resource.Drawable.cancel_notify, "REPLY", pIntent) 
      .SetAutoCancel(true); 
       //check bewlow 
       builder.SetDefaults((int)(NotificationDefaults.Sound | NotificationDefaults.Vibrate)); 

donc généralement je veux faire un bouton pour faire une notification au lieu de lancer une activité, etc.

Aidez-moi à ce suis nouveau dans Xamarin.android s'il vous plaît.

editted:

Ceci est mon code complet, selon la réponse que vous m'a fourni mais stilll me fais une exception:

using System; 
using Android.App; 
using Android.Content; 
using Android.Media; 
using Android.Support.V4.App; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Diagnostics; 
using System.Globalization; 
using Android.Widget; 

namespace Diabetes.Droid 
{ 
    [BroadcastReceiver] 
    [IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED" }, Priority = (int)IntentFilterPriority.LowPriority)] 
    public class AlarmReceiver : BroadcastReceiver 
    { 

     public override void OnReceive(Context context, Intent intent) 
     { 
      var message = intent.GetStringExtra("message"); 
      var title = intent.GetStringExtra("title"); 



      //Show toast here 
      Toast.MakeText(context, "Hello it's me ", ToastLength.Short).Show(); 
      var extras = intent.Extras; 

      if (extras != null && !extras.IsEmpty) 
      { 
       NotificationManager manager_ = context.GetSystemService(Context.NotificationService) as NotificationManager; 
       var notificationId = extras.GetInt("NotificationIdKey", -1); 
       if (notificationId != -1) 
       { 
        manager_.Cancel(notificationId); 
       } 
      } 

      AlarmReceiver customReceiver = new AlarmReceiver(); 

      //Create intent for action 1 (TAKE) 
      var actionIntent1 = new Intent(); 
      actionIntent1.SetAction("ARCHIVE"); 
      var pIntent1 = PendingIntent.GetBroadcast(context, 0, actionIntent1, PendingIntentFlags.CancelCurrent); 

      //Create intent for action 2 (REPLY) 
      var actionIntent2 = new Intent(); 
      actionIntent2.SetAction("REPLY"); 
      var pIntent2 = PendingIntent.GetBroadcast(context, 0, actionIntent2, PendingIntentFlags.CancelCurrent); 

      Intent resultIntent = context.PackageManager.GetLaunchIntentForPackage(context.PackageName); 
      /* 
      var resultIntent = new Intent(context, typeof(MainActivity)); 
      resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);*/ 

      var contentIntent = PendingIntent.GetActivity(context, 0, resultIntent, PendingIntentFlags.CancelCurrent); 




      var pending = PendingIntent.GetActivity(context, 0, 
       resultIntent, 
       PendingIntentFlags.CancelCurrent); 

      // Instantiate the Big Text style: 
      Notification.BigTextStyle textStyle = new Notification.BigTextStyle(); 


      var builder = 
       new Notification.Builder(context).SetContentTitle("Diabetics Reminder") 
           .SetDefaults(NotificationDefaults.Sound) 
           .AddAction(Resource.Drawable.tick_notify, "REPLY", pIntent1) 
           .AddAction(Resource.Drawable.cancel_notify, "ARCHIVE", pIntent2) 
           .SetSmallIcon(Resource.Drawable.ic_launcher).SetStyle(new Notification 
       .BigTextStyle() 
       .SetSummaryText("") 
                         .SetBigContentTitle(title) 
                         .BigText(message) 
      ) 
        .SetDefaults(NotificationDefaults.All); 

      builder.SetContentIntent(pending); 

      var notification = builder.Build(); 
      //Add intent filters for each action and register them on a broadcast receiver 
      var intentFilter = new IntentFilter(); 
      intentFilter.AddAction("ARCHIVE"); 
      intentFilter.AddAction("REPLY"); 

      context.RegisterReceiver(customReceiver, intentFilter); 

      var manager = NotificationManager.FromContext(context); 
      manager.Notify(10010, notification); 
     } 
    } 
} 

C'est l'erreur Image:

Error Image

Répondre

1

Vous avez besoin d'un récepteur de diffusion pour pouvoir afficher un Toast lorsque vous cliquez sur l'action. Pour chaque action, vous avez besoin d'une intention qui déclenche une intention de récepteur de diffusion. Voici un exemple sur la façon dont il ressemblerait à ceci:

========= BROADCAST RÉCEPTEUR CLASSE ==========

[BroadcastReceiver] 
public class CustomActionReceiver : BroadcastReceiver 
{ 
    public override void OnReceive(Context context, Intent intent) 
    { 
     //Show toast here 
     Toast.MakeText(context, intent.Action, ToastLength.Short).Show(); 
     var extras = intent.Extras; 

     if (extras != null && !extras.IsEmpty) 
     { 
      NotificationManager manager = context.GetSystemService(Context.NotificationService) as NotificationManager; 
      var notificationId = extras.GetInt("NotificationIdKey", -1); 
      if (notificationId != -1) 
      { 
      manager.Cancel(notificationId); 
      } 
     } 


    } 
} 

======= ========== CONSTRUIRE lA NOTIFICATION ============

  customReceiver = new CustomActionReceiver(); 

     //Create intent for action 1 (ARCHIVE) 
     var actionIntent1 = new Intent(); 
     actionIntent1.SetAction("ARCHIVE"); 
     var pIntent1 = PendingIntent.GetBroadcast(context, 0, actionIntent1, PendingIntentFlags.CancelCurrent); 

     //Create intent for action 2 (REPLY) 
     var actionIntent2 = new Intent(); 
     actionIntent2.SetAction("REPLY"); 
     var pIntent2 = PendingIntent.GetBroadcast(context, 0, actionIntent2, PendingIntentFlags.CancelCurrent); 


     Intent resultIntent = context.PackageManager.GetLaunchIntentForPackage(context.PackageName); 
     var contentIntent = PendingIntent.GetActivity(context, 0, resultIntent, PendingIntentFlags.CancelCurrent); 

     var builder = new NotificationCompat.Builder(context) 
     .SetContentIntent(contentIntent).SetSmallIcon(Resource.Drawable.ic_launcher) 
     .SetContentTitle(title) 
     .SetStyle(style).SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis()) 
     .AddAction(Resource.Drawable.tick_notify, "ARCHIVE", pIntent1) 
     .AddAction(Resource.Drawable.cancel_notify, "REPLY", pIntent2) 
     .SetAutoCancel(true); 

     builder.SetDefaults((int)(NotificationDefaults.Sound | NotificationDefaults.Vibrate)); 

     //Add intent filters for each action and register them on a broadcast receiver 
     var intentFilter = new IntentFilter(); 
     intentFilter.AddAction("ARCHIVE"); 
     intentFilter.AddAction("REPLY"); 

     context.RegisterReceiver(customReceiver, intentFilter); 

     NotificationManager notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService); 
     notificationManager.Notify(0, builder.Build()); 
+0

J'ai essayé de mettre en œuvre votre réponse @Rendy, mais je suis obtenir une erreur, alors s'il vous plaît vérifier mon code complet j'ai mis à jour mon post –

1

Vous ne pouvez pas enregistrer une intention de récepteur de radiodiffusion dans un autre récepteur de radiodiffusion. Essayez ceci:

using System; 
using Android.App; 
using Android.Content; 
using Android.Media; 
using Android.Support.V4.App; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Diagnostics; 
using System.Globalization; 
using Android.Widget; 

namespace Diabetes.Droid 
{ 
    [BroadcastReceiver] 
    [IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED" }, Priority = (int)IntentFilterPriority.LowPriority)] 
    public class AlarmReceiver : BroadcastReceiver 
    { 

    public override void OnReceive(Context context, Intent intent) 
    { 
     var message = intent.GetStringExtra("message"); 
     var title = intent.GetStringExtra("title"); 



     //Show toast here 
     Toast.MakeText(context, "Hello it's me ", ToastLength.Short).Show(); 
     var extras = intent.Extras; 

     if (extras != null && !extras.IsEmpty) 
     { 
      NotificationManager manager_ = context.GetSystemService(Context.NotificationService) as NotificationManager; 
      var notificationId = extras.GetInt("NotificationIdKey", -1); 
      if (notificationId != -1) 
      { 
       manager_.Cancel(notificationId); 
      } 
     } 

     //Create intent for action 1 (TAKE) 
     var actionIntent1 = new Intent(); 
     actionIntent1.SetAction("ARCHIVE"); 
     var pIntent1 = PendingIntent.GetBroadcast(context, 0, actionIntent1, PendingIntentFlags.CancelCurrent); 

     //Create intent for action 2 (REPLY) 
     var actionIntent2 = new Intent(); 
     actionIntent2.SetAction("REPLY"); 
     var pIntent2 = PendingIntent.GetBroadcast(context, 0, actionIntent2, PendingIntentFlags.CancelCurrent); 

     Intent resultIntent = context.PackageManager.GetLaunchIntentForPackage(context.PackageName); 
     /* 
     var resultIntent = new Intent(context, typeof(MainActivity)); 
     resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);*/ 

     var contentIntent = PendingIntent.GetActivity(context, 0, resultIntent, PendingIntentFlags.CancelCurrent); 

     var pending = PendingIntent.GetActivity(context, 0, 
      resultIntent, 
      PendingIntentFlags.CancelCurrent); 

     // Instantiate the Big Text style: 
     Notification.BigTextStyle textStyle = new Notification.BigTextStyle(); 


     var builder = 
      new Notification.Builder(context).SetContentTitle("Diabetics Reminder") 
          .SetDefaults(NotificationDefaults.Sound) 
          .AddAction(Resource.Drawable.tick_notify, "REPLY", pIntent1) 
          .AddAction(Resource.Drawable.cancel_notify, "ARCHIVE", pIntent2) 
          .SetSmallIcon(Resource.Drawable.ic_launcher).SetStyle(new Notification 
      .BigTextStyle() 
      .SetSummaryText("") 
                        .SetBigContentTitle(title) 
                        .BigText(message) 
     ) 
       .SetDefaults(NotificationDefaults.All); 

     builder.SetContentIntent(pending); 

     var notification = builder.Build(); 


     var manager = NotificationManager.FromContext(context); 
     manager.Notify(10010, notification); 
    } 
    } 

    [BroadcastReceiver] 
    [IntentFilter(new string[] { "ARCHIVE" , "REPLY" })] 
    public class CustomActionReceiver : BroadcastReceiver 
    { 
    public override void OnReceive(Context context, Intent intent) 
    { 
     //Show toast here 
     Toast.MakeText(context, intent.Action, ToastLength.Short).Show(); 
     var extras = intent.Extras; 

     if (extras != null && !extras.IsEmpty) 
     { 
     NotificationManager manager = context.GetSystemService(Context.NotificationService) as NotificationManager; 
     var notificationId = extras.GetInt("NotificationIdKey", -1); 
     if (notificationId != -1) 
     { 
      manager.Cancel(notificationId); 
     } 
     } 
    } 
    } 
} 
+0

Merci beaucoup @Rendy Del Rosario –