0

Anybody knows how to add notification once in sony smartwatch? I have followed the SampleNotificationExtension. As I know in SampleExtensionService, there are 2 methods related for starting insert an event:

    /**
     * Start periodic data insertion into event table
     */
    private void startAddData() {
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent i = new Intent(this, SampleExtensionService.class);
        i.setAction(INTENT_ACTION_ADD);
        PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),
                INTERVAL, pi);
    }

    /**
     * Cancel scheduled data insertion
     */
    private void stopAddData() {
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent i = new Intent(this, SampleExtensionService.class);
        i.setAction(INTENT_ACTION_ADD);
        PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
        am.cancel(pi);
    }

In the output the notification will be appeared but it will repeat every certain seconds. Anybody knows how to handle an event, so that I can show specific notification once?Thanks

eng
  • 83
  • 1
  • 9

2 Answers2

3

ExtensionService works like a other android Services. Just a send intent to your notification service via Context instance.

public class SampleExtensionService extends ExtensionService {
    public static final String EXTENSION_KEY = "sample.app";
    public static final String EXTENSION_SPECIFIC_ID = "notification.id";
    public static final String INTENT_ACTION_ADD = "add";
    public static final String EXTRA_EVENT_CONTENT = "extra.eventcontent";

    public SampleExtensionService() {
        super(EXTENSION_KEY);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int retVal = super.onStartCommand(intent, flags, startId);
        if ((intent != null) && (intent.getAction().equals(INTENT_ACTION_ADD))) {
            if (intent.getExtras() != null) {
                displayEvent(intent.getExtras());
            }
        }
        return retVal;
    }

    private void displayEvent(Bundle extras) {
        long sourceId = NotificationUtil.getSourceId(this, EXTENSION_SPECIFIC_ID);
        if (sourceId == NotificationUtil.INVALID_ID) {
            return;
        }

        ContentValues values = new ContentValues();
        values.put(Notification.EventColumns.DISPLAY_NAME, "New event");
        values.put(Notification.EventColumns.MESSAGE, extras.getString(EXTRA_EVENT_CONTENT));
        values.put(Notification.EventColumns.PERSONAL, 1);
        values.put(Notification.EventColumns.PUBLISHED_TIME, System.currentTimeMillis());
        values.put(Notification.EventColumns.SOURCE_ID, sourceId);

        getContentResolver().insert(Notification.Event.URI, values);
    }
}

From this activity we send intent for our service

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button sendButton = (Button) findViewById(R.id.send_button);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SampleExtensionService.class);
                intent.setAction(SampleExtensionService.INTENT_ACTION_ADD);

                Bundle extras = new Bundle();
                extras.putString(SampleExtensionService.EXTRA_EVENT_CONTENT, "Some event content");
                intent.putExtras(extras);

                startService(intent);
            }
        });
    }
}

For notification you must implement getSourceRegistrationConfigurations()

public class SampleRegistrationInformation extends RegistrationInformation {
    final Context mContext;

    protected SampleRegistrationInformation(Context context) {
        mContext = context;
    }

    @Override
    public int getRequiredNotificationApiVersion() {
        return 1;
    }

    @Override
    public ContentValues getExtensionRegistrationConfiguration() {
        <..>
    }

    @Override
    public ContentValues[] getSourceRegistrationConfigurations() {
        ContentValues sourceValues = new ContentValues();
        sourceValues.put(Notification.SourceColumns.ENABLED, true);
        sourceValues.put(Notification.SourceColumns.NAME, "Sample");
        sourceValues.put(Notification.SourceColumns.EXTENSION_SPECIFIC_ID, SampleExtensionService.EXTENSION_SPECIFIC_ID);
        sourceValues.put(Notification.SourceColumns.PACKAGE_NAME, mContext.getPackageName());
        return new ContentValues[]{sourceValues};
    }

}
goncharov
  • 43
  • 2
  • 6
  • Hi Goncharov, do you have an example what should I put in that if statement? – eng Oct 22 '13 at 07:29
  • Actually I can't start run the service in myActivity because it was different project between my phone app and notificationextension. But now I have solved that problem. Thank you for your help – eng Oct 24 '13 at 13:14
0

The only reason the notification continues to repeat is because the code is calling setRepeating in AlarmManager. If you only wanted to fire the event once you could just remove the repeating code. To show the notification itself take a look at the addData() method in the SampleExtensionService.java file.

mldeveloper
  • 2,253
  • 1
  • 13
  • 14
  • Hi Marlin, If I remove code for setRepeating in startAddData. I have got only one event is showing, the other events are not showing. Do you know why? – eng Oct 22 '13 at 08:10
  • Well that makes sense because if you don't repeat the Alarm then it should only add one event. If you want other events to show then you need to call the add event intent each time you want to add an event. – mldeveloper Oct 22 '13 at 18:33
  • Thank You Marlin, I followed your way. Btw, I have issue with vibrate. Why each notification I have received, there wasn't any vibrate? – eng Oct 24 '13 at 13:12
  • The watch should vibrate when you receive each notification. Does the vibrate work in other extensions? Have you tried restarting both the phone and watch? Do you have the latest version of SmartConnect and the Host app installed? – mldeveloper Oct 24 '13 at 18:40
  • I have checked with another phone, it works. In my extension I'm using Sony sdk 2.0. Before I was using phone with android version is 2.3 and with that phone my phone wasn't vibrating. But when I'm using phone with version 4.3 is working. Do you know why?Is it because the phone? – eng Oct 25 '13 at 09:52
  • I have fixed that. TI have to re install liveware.Thank you for your help Marlin! – eng Oct 25 '13 at 11:20