我的应用程序的通知不会弹出我的Android可穿戴手表。
据我所知,我不需要做任何事情就可以在Android可穿戴设备上显示通知。是不是?
其他应用程序的通知完美弹出。 有趣的是,在关闭其他应用程序的通知后,我的应用程序的通知会显示在前面!
另一个symtom:可穿戴式扩展操作应用于不可穿戴设备。
import android.app.Notification; import android.app.PendingIntent; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; import android.support.v4.app.RemoteInput; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; public class MainActivity extends AppCompatActivity { private static final String REPLY_ACTION = "342123"; private static final String EXIT_ACTION = "6454"; private static final String KEY_NOTIFICATION_ID = "1331133131"; private static final String KEY_MESSAGE_ID = "213213"; int NOTIFICATION_REQUEST_CODE = 11; String KEY_REPLY = "2323123"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("haha", ""); } public void onButtonClick(View view) { sendNotification(); } public void sendNotification() { NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "CHA"); builder .setSmallIcon(R.drawable.ic_launcher_foreground) .setContentTitle("Title") .setShowWhen(true) .extend(createWearableExtender()) .addAction(createReplyAction()) .addAction(createExitAction()) .setContentText("assdsdsdsda"); managerCompat.notify(NOTIFICATION_REQUEST_CODE, builder.build()); } private NotificationCompat.WearableExtender createWearableExtender() { NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender(); wearableExtender.addAction(createReplyAction()); wearableExtender.addAction(createExitAction()); wearableExtender.addPage(new Notification()); return wearableExtender; } private NotificationCompat.Action createReplyAction() { PendingIntent pendingIntent; Intent intent; RemoteInput remoteInput = new RemoteInput.Builder(KEY_REPLY) .setLabel("Reply Label 2") .build(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // start a // (i) broadcast receiver which runs on the UI thread or // (ii) service for a background task to b executed , but for the purpose of // this codelab, will be doing a broadcast receiver intent = new Intent(this, NotificationBroadcastReceiver.class); intent.setAction(REPLY_ACTION); intent.putExtra(KEY_NOTIFICATION_ID, NOTIFICATION_REQUEST_CODE); intent.putExtra(KEY_MESSAGE_ID, KEY_REPLY); pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); } else { // start your activity for Android M and below intent = new Intent(this, MainActivity.class); intent.setAction(REPLY_ACTION); intent.putExtra(KEY_MESSAGE_ID, KEY_REPLY); intent.putExtra(KEY_NOTIFICATION_ID, NOTIFICATION_REQUEST_CODE); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); } return new NotificationCompat.Action.Builder( R.drawable.ic_launcher_foreground, "Reply Label", pendingIntent) .addRemoteInput(remoteInput) .build(); } private NotificationCompat.Action createExitAction() { Intent intent; PendingIntent pendingIntent; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent = new Intent(this, NotificationBroadcastReceiver.class); intent.setAction(EXIT_ACTION); intent.putExtra(KEY_NOTIFICATION_ID, NOTIFICATION_REQUEST_CODE); intent.putExtra(KEY_MESSAGE_ID, KEY_REPLY); pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); } else { // start your activity for Android M and below intent = new Intent(this, MainActivity.class); intent.setAction(EXIT_ACTION); intent.putExtra(KEY_MESSAGE_ID, KEY_REPLY); intent.putExtra(KEY_NOTIFICATION_ID, NOTIFICATION_REQUEST_CODE); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); } return new NotificationCompat.Action.Builder( R.drawable.ic_launcher_foreground, "Exit", pendingIntent) .build(); } }
Manifest.xml看起来像这样:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".NotificationBroadcastReceiver" android:enabled="true" android:exported="false" /> </application> </manifest>