【发布时间】:2015-02-24 14:24:03
【问题描述】:
我尝试在用户进入或退出特定地理围栏时发送通知。 当用户进入或退出地理围栏时,我已经可以登录。但是发生这种情况时没有显示任何通知。有什么我想念的吗?我在清单文件中实现了一个主题。
我还更新了我的清单,例如:
<activity
android:name=".HomeActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
这是我的 GeofenceIntentService,
public class GeofenceIntentService extends IntentService {
private NotificationManager mNotificationManager;
public static final int NOTIFICATION_ID = 1;
public static final String TRANSITION_INTENT_SERVICE = "ReceiveTransitionsIntentService";
public GeofenceIntentService() {
super(TRANSITION_INTENT_SERVICE);
}
@Override
protected void onHandleIntent(Intent intent) {
if (LocationClient.hasError(intent)) {
//todo error process
} else {
int transitionType = LocationClient.getGeofenceTransition(intent);
if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER ||
transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) {
List<Geofence> triggerList = LocationClient.getTriggeringGeofences(intent);
for (Geofence geofence : triggerList) {
String locationId = geofence.getRequestId();
int value = transitionType;
String geofenceStatus;
if(value == 1){
Log.i("**************************************", "Entered geofence");
sendNotification("entering");
}else if(value == 2){
Log.i("**************************************", "Exiting geofence");
sendNotification("exiting");
}
}
}
}
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, HomeActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
任何人有一些想法如何让它工作?我的主要活动类是 HomeActivity。
【问题讨论】:
-
您没有为您的通知分配任何通知渠道。如果通知未与任何频道关联,则不会出现通知。这是阅读此内容的链接=>> developer.android.com/training/notify-user/channels#java 另外,我的建议是按照本教程添加通知(阅读有关如何仔细添加通知渠道的主题)
标签: android intentservice geofencing android-geofence