Creating Activities
PUBLISHED
You need to create the main activity with the Notification Message Manager to send and receive messages. In addition, you need activities to manage the connection between the Android and Gear devices.
To manage the connection and message sending:
- Build the RichNotificationActivity.java main activity.
The following instructions only cover the major features. For more detail, see the full source code.
- Using the Srn class:
- initialize() function initializes Srn. You must initialize the Srn package before you can use it. If the device does not support RichNotification, the SsdkUnsupportedException exception is thrown.
- getVersionCode() function gets the RichNotification version number as an integer.
- getVersionName() function gets the RichNotification version name as a string.
- isFeatureEnabled() function checks whether an Srn package feature is available on the device.
- Using the RichNotification package:
-
Creating the Rich Notification Manager
First, create the RichNotificationManager instance that is used to send the notification to the wearable device:
mRichNotificationManager = new SrnRichNotificationManager(getApplicationContext());
-
Starting the Rich Notification Manager
After you create the RichNotificationManager instance, call the start() function to notify or dismiss the notification on the wearable device:
mRichNotificationManager.start();
-
Stopping the Rich Notification Manager
When your application is terminated or the manager must be stopped, call the stop() function, if the manager has started:
mRichNotificationManager.stop();
-
Checking Rich Notification availability
If you want to check the connection of the wearable device which supports Rich Notification, use the isConnected() function:
mRichNotificationManager.isConnected();
-
Sending a Rich Notification
Once the Rich Notification is set up, use the RichNotificationManager instance to send it to the RichNotificationService. This call returns a UUID for the notification that you can store in your application to use in the future if you want to update or dismiss a previously sent rich notification.
Note Since Rich Notifications can persist on the wearable device, store your notification UUID in a persistent data store on the host device to ensure that it survives a host device reboot. UUID uuid = mRichNotificationManager.notify(example.createRichNoti());
Note If a Rich Notification from your application is not displayed in the device.In “Samsung Gear” of the mobile device, go to Notifications and make sure the Limit notifications menu is unchecked. If this is checked and the mobile device is being used, notifications are not shown in the Gear device.
-
package com.samsung.android.myfirstrichnotification; import android.net.Uri; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.Spinner; import android.widget.Toast; import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.common.api.GoogleApiClient; import com.samsung.android.sdk.SsdkUnsupportedException; import com.samsung.android.sdk.richnotification.Srn; import com.samsung.android.sdk.richnotification.SrnRichNotificationManager; import com.samsung.android.sdk.richnotification.SrnRichNotificationManager.ErrorType; import com.samsung.android.sdk.richnotification.SrnRichNotificationManager.EventListener; import java.util.UUID; public class RichNotificationActivity extends AppCompatActivity implements EventListener { public enum TemplateTypes { SMALL_HEADER, MEDIUM_HEADER, LARGE_HEADER, FULL_SCREEN, EVENT, IMAGE; } private SrnRichNotificationManager mRichNotificationManager; private Spinner mSpinner; /** * ATTENTION: This was auto-generated to implement the App Indexing API * See https://g.co/AppIndexing/AndroidStudio for more information */ private GoogleApiClient client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_rich_notification); Srn srn = new Srn(); try { // Initialize an instance of Srn srn.initialize(this); } catch (SsdkUnsupportedException e) { // Error handling } mRichNotificationManager = new SrnRichNotificationManager(getApplicationContext()); mSpinner = (Spinner) findViewById(R.id.spinner1); Toast.makeText(this, "isConnected : " + mRichNotificationManager.isConnected(), Toast.LENGTH_LONG).show(); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); /* * FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); * fab.setOnClickListener(new View.OnClickListener() { * @Override * public void onClick(View view) { * Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) * .setAction("Action", null).show(); * } * }); */ // ATTENTION: This was auto-generated to implement the App Indexing API // See https://g.co/AppIndexing/AndroidStudio for more information client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); } @Override protected void onResume() { super.onResume(); mRichNotificationManager.start(); mRichNotificationManager.registerRichNotificationListener(this); } @Override protected void onPause() { super.onPause(); mRichNotificationManager.unregisterRichNotificationListener(this); mRichNotificationManager.stop(); } public void onSendClick(View v) { perform(mSpinner.getSelectedItemPosition()); } public void perform(int primary) { if (primary < 0 || primary >= TemplateTypes.values().length) { return; } Toast.makeText(RichNotificationActivity.this, "Sending Notification ...", Toast.LENGTH_SHORT).show(); switch (TemplateTypes.values()[primary]) { case SMALL_HEADER: performExample(new SmallHeaderExample(getApplicationContext())); break; case MEDIUM_HEADER: performExample(new MediumHeaderExample(getApplicationContext())); break; case LARGE_HEADER: performExample(new LargeHeaderExample(getApplicationContext())); break; case FULL_SCREEN: performExample(new FullScreenExample(getApplicationContext())); break; case EVENT: performExample(new EventExample(getApplicationContext())); break; case IMAGE: performExample(new ImageExample(getApplicationContext())); break; } } private void performExample(IExample example) { UUID uuid = mRichNotificationManager.notify(example.createRichNoti()); Toast.makeText(RichNotificationActivity.this, "Notification Id : " + uuid, Toast.LENGTH_SHORT).show(); } @Override public void onError(UUID arg0, ErrorType arg1) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "Something wrong with uuid" + arg0.toString() + "Error:" + arg1.toString(), Toast.LENGTH_LONG).show(); } @Override public void onRead(UUID arg0) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "Read uuid" + arg0.toString(), Toast.LENGTH_LONG).show(); } @Override public void onRemoved(UUID arg0) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "Removed uuid" + arg0.toString(), Toast.LENGTH_LONG).show(); } }
- Using the Srn class:
- Create 3 additional activities to manage the connection:
- In order to check out the connection between Android mobile device and Samsung Gear, a DeviceStateReceiver.java file must be implemented. If devices are connected to each other successfully, a success message pops up in the Android mobile device.
Figure: Connection check
Create the DeviceStateReceiver.java file with the following content:
Figure: DeviceStateReceiver.java
package com.samsung.android.myfirstrichnotification; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; import android.widget.Toast; public class DeviceStateReceiver extends BroadcastReceiver { private static final String TAG = DeviceStateReceiver.class.getSimpleName(); @Override public void onReceive(Context context, Intent intent) { boolean isConnected = intent.getBooleanExtra("isConnected", false); Log.d(TAG, "Connected : " + isConnected); Toast.makeText(context, "Connected : " + isConnected, Toast.LENGTH_LONG).show(); } }
- To manage the callback message, the MyCallbackReceiver.java and MyCallbackActivity.java files must be implemented.
Create the files with the following content:
Figure: MyCallbackReceiver.java
package com.samsung.android.myfirstrichnotification; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyCallbackReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String data = intent.getStringExtra("extra_action_data"); if (data != null) { Toast.makeText(context, data, Toast.LENGTH_SHORT).show(); } } }
Figure: MyCallbackActivity.java
package com.samsung.android.myfirstrichnotification; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; public class MyCallbackActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); String data = intent.getStringExtra("extra_action_data"); if (data != null) { Toast.makeText(this, data, Toast.LENGTH_SHORT).show(); } } }
- In order to check out the connection between Android mobile device and Samsung Gear, a DeviceStateReceiver.java file must be implemented. If devices are connected to each other successfully, a success message pops up in the Android mobile device.
- Modify the AndroidManifest.xml file:
- Add the permissions. If these are not added in the AndroidManifest.xml file, the initialization fails with SecurityException.
- Add the additional activity elements and receiver elements.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.samsung.android.myfirstrichnotification"> <uses-permission android:name="com.samsung.wmanager.ENABLE_NOTIFICATION" /> <uses-permission android:name="com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.SEND_SMS" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".RichNotificationActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- ATTENTION: This was auto-generated to add Google Play services to your project for App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information --> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name="com.samsung.android.myfirstrichnotification.MyCallbackActivity" > <intent-filter> <action android:name="android.intent.action.VIEW" /> </intent-filter> </activity> <receiver android:name="com.samsung.android.myfirstrichnotification.MyCallbackReceiver" > <intent-filter> <action android:name="com.samsung.android.myfirstrichnotificatione.callback_broadcast" /> </intent-filter> </receiver> <receiver android:name="com.samsung.android.myfirstrichnotification.DeviceStateReceiver" > <intent-filter> <action android:name="com.samsung.wmanager.rich_notification.DEVICE_STATE_CHANGED" /> </intent-filter> </receiver> </application> </manifest>
- Set up the Gear Manager on the Android mobile device.
To receive a message on the Samsung Gear device, you need to uncheck the Limit Notifications in a Gear Manager application in the Android mobile device.
Figure: Uncheck Limit notifications
Was this document helpful?
We value your feedback. Please let us know what you think.