如何知道推开的时间; Azure推送通知通知中心?

时间:2016-08-04 19:25:18

标签: java android azure push-notification azure-notificationhub

我使用Azure的通知中心实现了推送通知。我注意到在扩展NotificationHandler时它们只有三种覆盖方法:

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());
    }
}

1 个答案:

答案 0 :(得分: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()中检查该信息。

,当没有调用推送通知时,它将为空(null),否则它将为您提供信息。

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