【问题标题】:How to know when push is opened; Azure Push Notifications Notification Hub?如何知道推送何时打开; Azure 推送通知通知中心?
【发布时间】:2016-12-11 01:48:21
【问题描述】:

我已经使用 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());
    }
}

【问题讨论】:

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


    【解决方案1】:

    对于其他试图弄清楚这一点的人来说,这实际上非常简单。在 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.
    

    【讨论】:

      猜你喜欢
      • 2018-04-28
      • 1970-01-01
      • 2019-08-25
      • 2018-05-06
      • 2022-01-12
      • 2015-05-12
      • 2017-06-05
      • 2015-12-11
      • 2021-08-03
      相关资源
      最近更新 更多