gpt4 book ai didi

android - 如何在 IntentService 中启动通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:58:01 27 4
gpt4 key购买 nike

我尝试在用户进入或退出特定地理围栏时发送通知。我已经可以在用户进入或退出地理围栏时进行记录。但是发生这种情况时没有显示任何通知。有什么我想念的吗?我在我的 list 文件中实现了一个主题。

我还更新了我的 list :

 <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());
}

}

有人知道如何让它工作吗?我的主要 Activity 类是 HomeActivity。

最佳答案

尝试向您的通知构建器添加一个图标:

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setSmallIcon(R.drawable.notification_icon);

关于android - 如何在 IntentService 中启动通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28698278/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com