【问题标题】:How to start a notification in IntentService如何在 IntentService 中启动通知
【发布时间】: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。

【问题讨论】:

标签: android intentservice geofencing android-geofence


【解决方案1】:

尝试在通知生成器中添加图标:

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

【讨论】:

  • 这应该是正确的。 Afaik,通知不会在没有图标的情况下显示。
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多