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

查看:176
本文介绍了适用于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推送通知(除非您有办法在服务器端过滤掉其中的一些用户).然后,在您的应用程序中,您不仅要创建一个Notification并将其传递给Notification Manager,还需要首先使用LocationManager(或更新的LocationServices API)来确定用户是否首先位于正确的位置,然后才丢弃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.

您需要注意以下几点:

  • 您的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" />

  • 您还需要在清单的<application>部分中设置一个Notification Receiver:

  • 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天全站免登陆