2016-08-04 199 views
0

我已经使用Azure的通知中心实施了推送通知。我注意到,他们只有三个重写方法延长NotificationHandler时:如何知道何时打开按钮; Azure推送通知通知中心?

public void onReceive(Context context, Bundle bundle) 

public void onUnregistered(Context context, String gcmRegistrationId) 

public void onRegistered(Context context, String gcmRegistrationId) 

大多数推送通知服务有当上了推送通知用户按他们收到这称为onPushOpen()或的OnOpen()回调方法。如何知道某人何时点击了收到的推送通知?我正在试验演示。一切工作都减去了我所说的关切。

链接:https://github.com/Azure/azure-notificationhubs-samples/blob/master/Android/GetStartedFirebase/app/src/main/java/com/example/microsoft/getstartednh/MyHandler.java

public class MyHandler extends NotificationsHandler { 
    public static final int NOTIFICATION_ID = 1; 
    private NotificationManager mNotificationManager; 
    NotificationCompat.Builder builder; 
    Context ctx; 

    @Override 
    public void onReceive(Context context, Bundle bundle) { 
     ctx = context; 
     String nhMessage = bundle.getString("message"); 
     sendNotification(nhMessage); 
     if (MainActivity.isVisible) { 
      MainActivity.mainActivity.ToastNotify(nhMessage); 
     } 
    } 

    private void sendNotification(String msg) { 

     Intent intent = new Intent(ctx, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     mNotificationManager = (NotificationManager) 
       ctx.getSystemService(Context.NOTIFICATION_SERVICE); 

     PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, 
       intent, PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(ctx) 
         .setSmallIcon(R.mipmap.ic_launcher) 
         .setContentTitle("Notification Hub Demo") 
         .setStyle(new NotificationCompat.BigTextStyle() 
           .bigText(msg)) 
         .setSound(defaultSoundUri) 
         .setContentText(msg); 

     mBuilder.setContentIntent(contentIntent); 
     mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
    } 
} 

回答

0

其他任何人试图弄清楚这一点,它实际上是非常简单的。在sendNotification()方法中,无论打算推送通知何时要跟踪的信息,都会将其作为额外事件添加到您的意图中。例如。

private void sendNotification(String msg) { 

     Intent intent = new Intent(ctx, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     // seeking to do something specific when notification is opened if flag is set 
     intent.putExtra("onPushOpen", "performSomeAction"); 

     mNotificationManager = (NotificationManager) 
       ctx.getSystemService(Context.NOTIFICATION_SERVICE); 

     PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, 
       intent, PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(ctx) 
         .setSmallIcon(R.mipmap.ic_launcher) 
         .setContentTitle("Notification Hub Demo") 
         .setStyle(new NotificationCompat.BigTextStyle() 
           .bigText(msg)) 
         .setSound(defaultSoundUri) 
         .setContentText(msg); 

     mBuilder.setContentIntent(contentIntent); 
     mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
    } 

现在你已经做到了。在onResume()检查这些信息。如果未触发推送通知,它将为空(空),否则它将提供您的信息。

Bundle b = getIntent().getExtras(); 
// now retrieve what you want from the bundle.