Question

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

Was it helpful?

Solution 2

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.

OTHER TIPS

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

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top