适用于 Android 的基于位置的推送通知 [英] Location Based Push Notifications For Android

查看:20
本文介绍了适用于 Android 的基于位置的推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不使用 Parse 等第三方推送通知服务的情况下为 android 设备发送基于位置的推送通知?我想向我的用户发送推送通知,而不会因为他们不在某个区域而收到与该特定用户无关的通知.此外,我可以根据时间间隔获取用户位置,但如果可能,我宁愿采用不同的方式.

Is there anyways in sending a location based push notification for android devices with out using a third party push notification service such as Parse? I would like to send push notification to my users without the annoyance of getting a notification that doesn't relate to that specific user because they are not in a certain area. Also, I could get the users location based on a time interval but I would rather do it a different way then that, if possible.

推荐答案

是的,这是完全可能的,只要我正确地解释了您的要求.

Yes, this is entirely possible, as long as I'm correctly interpreting what you are asking.

要完成此操作,您需要向所有用户发送 GCM 推送通知(除非您有办法在服务器端过滤掉其中的一些用户).然后在您的应用程序中,您将首先使用 LocationManager(或较新的 LocationServices API)来确定用户是否在正确的位置内,而不是仅仅创建一个 Notification 并将其传递给通知管理器,然后丢弃 GCM如果他们不是,则通知.

To accomplish this, you would send the GCM push notification to all of your users (unless you had a way, server-side of filtering some of them out). Then in your application, instead of just creating a Notification and passing it to the notification manager, you would first use the LocationManager (or the newer LocationServices API) to determine if the user is within the proper location first, and then just discard the GCM notification if they're not.

要做到这一点,您需要注意以下几点:

You'll need to take care of several things in order to do this:

  • 您的 AndroidManifest.xml 将需要进行多项权限更改,包括 GCM 更改和位置访问权限:

  • Your AndroidManifest.xml will require several permission changes, both for the GCM changes, and for the Location access:

<!-- Needed for processing notifications -->
<permission android:name="com.myappname.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.myappname.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<!--  Needed for Location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

  • 您还需要在清单的 部分设置通知接收器:

  • You'll also need to set up a Notification Receiver in the <application> section of the manifest:

    <receiver android:name="com.myappname.NotificationReceiver" android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.myappname" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.myappname" />
        </intent-filter>
    </receiver>
    

  • 此外,您还需要编写 NotificationReceiver java 类,并覆盖 onReceive 函数:

    public class NotificationReceiver extends BroadcastReceiver {
        public void onReceive(final Context context, final Intent intent) {
    
            if ("com.google.android.c2dm.intent.REGISTRATION".equals(intent.getAction())) {
    
                handleRegistration(context, intent); // you'll have to write this function
    
            } else if ("com.google.android.c2dm.intent.RECEIVE".equals(intent.getAction())) {
    
                // the handle message function will need to check the user's current location using the location API you choose, and then create the proper Notification if necessary.
                handleMessage(context, intent);
    
            }
    }
    

  • 这篇关于适用于 Android 的基于位置的推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆