如何在 IntentService 中启动通知

How to start a notification in IntentService

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

我还更新了清单,例如:

 <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 class 是 HomeActivity。

尝试向您的通知生成器添加一个图标:

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