2016-08-04 181 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.