简体   繁体   中英

Android Wear: Custom Page on Notification

I'm very new to Android Wear (development). I started reading and implementing the documentation .

However I'm not sure if what I want to implement is "überhaupt" possible. I can attach custom "actions" on the push notifications I receive, but it seems it can only open a phone-activity. Why can't I open a wear-activity?

The push notifications contains text, which is initially displayed, and data about a soccer match (second page?). I want to display the names of the teams and the score without an intervention of the phone.

So is it possible?

Plus what is the default behaviour? Do I attach this to an action or via an extra page on the notification?

NotificationCompat.Builder notificationBuilder =
    new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_notification_icon3)
            .setContentTitle(this.getString(R.string.notifications_title))
            .setContentText(message)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setContentIntent(matchDetailPendingIntent)
            .setAutoCancel(true)
            .extend(new NotificationCompat.WearableExtender()
                            .addPage(CustomDesignedPage) //Is this possible?
                            .addAction(action)
                            .setBackground(BitmapFactory.decodeResource(getResources(), R.drawable.soccer_background_big))
            );

EDIT

Looking at the Messenger wear-app it seems possible? Messenger Android Wear应用

The second screen shows a list of messages for example.

I was having the same problem. My solution was to implement an Activity and add to this activity the custom layout. Follow this step.

Step 1: Create a custom layout in your wear module. Example: customlayout.xml

Step 2: Create an Activity in the wear module:

    public class WearNotificationActivity extends Activity{

    private ImageView mSomeButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.customlayout);

        mSomeButton= (ImageView) this.findViewById(R.id.somebutton);



        mSomeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //do something here
            }
        });

    }

}

Step 3. Send the data you want from your phone to your wear:

    public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{

     private GoogleApiClient mGoogleApiClient;
     private Button mSomeButton;

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mainlayout);
            mSomeButton=(Button) findViewById(R.id.somebutton);

            mGoogleApiClient = new GoogleApiClient.Builder(this)
                            .addApi(AppIndex.APP_INDEX_API)
                            .addApi(Wearable.API)
                            .addConnectionCallbacks(this)
                            .addOnConnectionFailedListener(this)
                            .build();
            mSomeButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
               sendToWear("title","description");
            }
        });

    }
    @Override
        public void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            if(!mGoogleApiClient.isConnected()) {
                mGoogleApiClient.connect();
            }

        }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mGoogleApiClient!=null) {
            mGoogleApiClient.disconnect();
        }    

    }
    public void sendToWear(String title, String description){        
        PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/wear");
        putDataMapReq.getDataMap().putString("title", title);
        putDataMapReq.getDataMap().putString("description", description);                
        Wearable.DataApi.putDataItem(mGoogleApiClient, putDataRequest);
    }

}

Step 4. Receive the data in your wear and make the notification. For do this you have to create a class in the wear module that extends for WearableListenerService and add this class to your wear manifest.

 public class NotificationUpdateService extends WearableListenerService
        implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
        ResultCallback<DataApi.DeleteDataItemsResult> {

    private GoogleApiClient mGoogleApiClient;

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


    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        for (DataEvent dataEvent : dataEvents) {
            if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
                DataItem item = dataEvent.getDataItem();
                if (item.getUri().getPath().compareTo("/wear") == 0) {
                    DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
                    String title = dataMap.getString("title");
                    String description=dataMap.getString("description");
                    buildWearableOnlyNotification(title, description)

                }
            } else if (dataEvent.getType() == DataEvent.TYPE_DELETED) {

            }
        }
    }

    /**
     * Builds a simple notification on the wearable.
     */
    private void buildWearableOnlyNotification(String title, String content) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setOngoing(true)
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    .setVibrate(new long[]{10, 10, 10, 10, 10})
                    .setContentTitle(title)
                    .setContentText(content);
            Intent notificationIntent = new Intent(this, WearNotificationActivity.class);
            PendingIntent pendingNotificationIntent =
                    PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder secondpage =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .extend(new NotificationCompat.WearableExtender()
                                            .setDisplayIntent(pendingNotificationIntent)
                                            .setCustomSizePreset(NotificationCompat.WearableExtender.SIZE_FULL_SCREEN)
                            );            
            mNotificationBuilder = new NotificationCompat.WearableExtender()
                    .addPage(secondpage.build()).extend(builder);
            Notification notification=mNotificationBuilder.build();
            ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
                    .notify(Constants.WATCH_ONLY_ID, notification);
    }

    @Override
    public void onConnected(Bundle bundle) {

    }

    @Override
    public void onConnectionSuspended(int i) {
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
    }


}

And in your manifest:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.DeviceDefault" >
    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".WearNotificationActivity"
        android:exported="true"
        android:allowEmbedded="true"
        android:taskAffinity=""
        android:theme="@android:style/Theme.DeviceDefault.Light"
        >
    </activity>
    <service android:name=".NotificationUpdateService">
        <intent-filter>
            <action
                android:name="com.google.android.gms.wearable.BIND_LISTENER" />
        </intent-filter>
    </service>
</application>

Finally you need to add all the dependencies in your phone and wear gradle.

Phone:

compile 'com.google.android.gms:play-services-wearable:7.5.0'
compile 'com.android.support:support-v4:23.1.0'
wearApp project(':wearmodule')

Wear:

compile 'com.google.android.support:wearable:1.3.0'
provided 'com.google.android.wearable:wearable:+'
compile 'com.google.android.gms:play-services-wearable:8.1.0'

I hope this was useful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM