2015-02-24 118 views
3

我尝试在用户进入或退出特定地理围栏时发送通知。 我已经可以在用户进入或退出地理围栏时进行登录。但是,如果发生这种情况,通知不会显示有什么我失踪?我在manifest文件中实现了一个主题。如何在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()); 
    } 

} 

任何人有一些想法如何得到这个工作?我的主要活动课是HomeActivity。

+0

的可能重复[?如何显示在状态栏通知(http://stackoverflow.com/questions/13901657/how-to-show -notification-in-status-bar) – 2015-02-24 20:07:15

回答

8

尝试增加一个图标到您的通知建设者:

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

这应该是正确的。 Afaik,通知不会显示没有图标。 – 2015-02-24 14:33:23