How to start a notification in IntentService

Theo Jansen picture Theo Jansen · Feb 24, 2015 · Viewed 11.3k times · Source

I'm try to send a notification when a user enters or exits a specific geofence. I already can log when a user enters or exits the geofence. But no notification is showed up when this happened. Is there anything i'm missing? I implemented a theme in my manifestfile.

I also updated my manifest like :

 <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>

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

}

Anyone have some ideas how to get this working? My main activity class is HomeActivity.

Answer

Micky picture Micky · Feb 24, 2015

Try adding an icon to your notification builder:

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