package com.doubleencore.sample.wear.ongoingnotification;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.data.FreezableUtils;
import com.google.android.gms.wearable.Asset;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.Wearable;
import com.google.android.gms.wearable.WearableListenerService;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by carlos on 7/15/14.
 */
public class OngoingNotificationListenerService extends WearableListenerService {
    private static final String TAG = OngoingNotificationListenerService.class.getSimpleName();

    private GoogleApiClient mGoogleApiClient;
    private int notificationId = 100;

    @Override
    public void onCreate() {
        super.onCreate();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .build();
        mGoogleApiClient.connect();
    }

    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
        dataEvents.close();

        if (!mGoogleApiClient.isConnected()) {
            ConnectionResult connectionResult = mGoogleApiClient
                    .blockingConnect(30, TimeUnit.SECONDS);
            if (!connectionResult.isSuccess()) {
                Log.e(TAG, "Service failed to connect to GoogleApiClient.");
                return;
            }
        }

        for (DataEvent event : events) {
            if (event.getType() == DataEvent.TYPE_CHANGED) {
                String path = event.getDataItem().getUri().getPath();
                if (Constants.PATH_NOTIFICATION.equals(path)) {
                    // Get the data out of the event
                    DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
                    final String title = dataMapItem.getDataMap().getString(Constants.KEY_TITLE);
                    Asset asset = dataMapItem.getDataMap().getAsset(Constants.KEY_IMAGE);

                    // Build the intent to display our custom notification
                    Intent notificationIntent = new Intent(this, NotificationActivity.class);
                    notificationIntent.putExtra(NotificationActivity.EXTRA_TITLE, title);
                    notificationIntent.putExtra(NotificationActivity.EXTRA_IMAGE, asset);
                    PendingIntent notificationPendingIntent = PendingIntent.getActivity(
                            this,
                            0,
                            notificationIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT);

                    // Create the ongoing notification
                    Notification.Builder notificationBuilder = new Notification.Builder(this)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
                            .setOngoing(true)
                            .extend(new Notification.WearableExtender()
                                    .setDisplayIntent(notificationPendingIntent));
                    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

                    // Build the notification and show it
                    notificationManager.notify(notificationId, notificationBuilder.build());
                } else {
                    Log.d(TAG, "Unrecognized path: " + path);
                }
            }
        }
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        if (messageEvent.getPath().equals(Constants.PATH_DISMISS)) {
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
            notificationManager.cancel(notificationId);
        }
    }
}

@@ -85,16 +85,56 @@
 cation;%0A
+import android.app.NotificationManager;%0A
 import a
@@ -1130,24 +1130,76 @@
 impleName();
+%0A    private static final int NOTIFICATION_ID = 100;
 %0A%0A    privat
@@ -1237,46 +1237,8 @@
 ent;
-%0A    private int notificationId = 100;
 %0A%0A  
@@ -3715,24 +3715,25 @@
 ngIntent));%0A
+%0A
             
@@ -3732,33 +3732,46 @@
                 
-N
+// Build the n
 otificationManag
@@ -3761,39 +3761,58 @@
 notification
-ManagerCompat n
+ and show it%0A                    N
 otificationM
@@ -3810,35 +3810,33 @@
 ficationManager 
-= N
+n
 otificationManag
@@ -3841,62 +3841,13 @@
 ager
-Compat.from(this);%0A%0A                    // Build the n
+ = (N
 otif
@@ -3849,36 +3849,72 @@
 Notification
- and show it
+Manager) getSystemService(NOTIFICATION_SERVICE);
 %0A           
@@ -3949,30 +3949,31 @@
 .notify(
-notificationId
+NOTIFICATION_ID
 , notifi
@@ -4419,22 +4419,23 @@
 cel(
-notificationId
+NOTIFICATION_ID
 );%0A 

Use NotificationManager