basteez
basteez

Reputation: 477

Custom Notification doesn't run on Android Wear

I'm playing with Wear SDK and trying to create a wear application. I want to display a custom notification as shown on android docs but it doesn't work.

This is my Activity Code:

public class WearActivity extends Activity {
    private Button notifyBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wear);
        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);

        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {

            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                notifyBtn = (Button) stub.findViewById(R.id.notifyBtn);
                notifyBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        createNotification();

                    }
                });
            }
        });
    }

    private void createNotification(){
        Toast.makeText(this, "Press", Toast.LENGTH_LONG).show();
        //Create Intent
        Intent notificationIntent =
                new Intent(this, WearNotificationActivity.class)
                        .putExtra("EXTRA_STRING", "Hi, I'm an EXTRA!");
        PendingIntent pendingNotificationIntent =
                PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification =
                new Notification.Builder(this)
                        .extend(new Notification.WearableExtender()
                                .setDisplayIntent(pendingNotificationIntent)
                                .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
                        .build();

        NotificationManager notificationManager =
                (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(0, notification);
    }
}

Although the button press is triggered, no Custom notification appears. I edited the Manifest Too:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tizianobasile.wearactivity" >

    <uses-feature android:name="android.hardware.type.watch" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.DeviceDefault.Light" >
        <activity
            android:name=".WearActivity"
            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:label="@string/title_activity_wear_notification"
            android:exported="true"
            android:allowEmbedded="true"
            android:taskAffinity=""
            android:theme="@android:style/Theme.DeviceDefault.Light">
        </activity>
    </application>

</manifest>

I really can't find the cause, my code is almost identical to the documentation code.

Upvotes: 0

Views: 580

Answers (1)

Maciej Ciemięga
Maciej Ciemięga

Reputation: 10713

In general, the smallIcon is mandatory for a notification in Android Wear. From my tests, despite that in a notification with custom card layout (setDisplayIntent) the icon is not even displayed - you still need to specify it in order to appear on Android Wear at all.

For example:

Notification notification =
    new Notification.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .extend(new Notification.WearableExtender()
            .setDisplayIntent(pendingNotificationIntent)
            .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
        .build();

Upvotes: 3

Related Questions