有没有办法在不使用第三方推送通知服务(例如 Parse)的情况下为 Android 设备发送基于位置的推送通知?我想向我的用户发送推送通知,而不会因为收到与该特定用户无关的通知而烦恼,因为他们不在某个区域。另外,我可以根据时间间隔获取用户位置,但如果可能的话,我宁愿采用不同的方式。
是的,这是完全可能的,只要我正确解释你所问的内容。
为了实现这一点,您将向所有用户发送 GCM 推送通知(除非您有办法在服务器端过滤掉其中一些用户)。然后在您的应用程序中,您不只是创建通知并将其传递给通知管理器,而是首先使用 LocationManager(或较新的 LocationServices API)首先确定用户是否位于正确的位置,然后丢弃 GCM如果不是,请通知。
为此,您需要注意几件事:
-
您的 AndroidManifest.xml 将需要进行多项权限更改,包括 GCM 更改和位置访问:
<!-- 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>
清单部分:
<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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)