【问题标题】:To generate push notification based on user current device location in android在android中根据用户当前设备​​位置生成推送通知
【发布时间】:2015-09-11 13:11:45
【问题描述】:

我能够从服务器获取推送通知,但是当用户设备位置的经纬度发生变化时,我的应用应该会收到推送通知。

案例 1:

我必须将设备纬度和经度值发送到服务器,并且它必须在用户设备当前位置发生变化时发送推送通知。(从地方 A 移动到地方 B 以获取通知)即使应用程序在后台运行,应用程序需要配置系统操作系统才能推送。

案例 2:

我能够在单击推送通知时移动到活动,但我需要像如果我从服务器收到两个推送通知,单击第一个通知它必须采取 FirstActivity 并单击第二个推送通知采取 secondActivity。 如何识别要重定向到活动的推送通知。

GCMNotificationIntentService 中显示推送通知的代码:

private void createNotification(String msg){
    Log.d(TAG, "Preparing to send notification...: " + msg);
    NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.offer, "GCM Notification", System.currentTimeMillis());
    Intent notificationIntent = new Intent(this, FirstActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(this, NOTIFICATION_ID,notificationIntent, 0);
    notification.setLatestEventInfo(this, "Message From GCM", msg, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(NOTIFICATION_ID, notification);
    NOTIFICATION_ID = NOTIFICATION_ID + 1;
}

【问题讨论】:

    标签: android android-studio push-notification location


    【解决方案1】:

    1.您需要创建将在后台运行的服务。您还需要以某种方式从您的服务器向电话发送通知并进行处理。但它可以在没有服务器的情况下实现,你可以只听 LocationListener.OnLocationChanged

    2.根据第二个问题你可以在显示通知前准备好Intent,分配给通知,点击后直接跳转到这个activity。

    Intent intent = //prepare your intent
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setContentIntent(contentIntent);
    notificationManager.notify(notificationId, builder.build());
    

    【讨论】:

    • 感谢@maciej 的回复,如果您不介意,请分享一些代码以移动到推送通知的不同活动以及案例 1(服务呼叫位置更新)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多