Different text for the wear device and the mobile device

前端 未结 3 1048
灰色年华
灰色年华 2020-12-12 01:11

Is it possible to have a notification that shows a different text (content title and content text) in the Android wear device and in the mobile device?

3条回答
  •  孤城傲影
    2020-12-12 01:42

    Yes, it's possible now with little tricky and bug on Android wear which help's us. More Over this trick doesn't need Android wear API, just a normal Notification with RemoteViews is enough.

    NotificationCompat.Builder mBuilder;
        mBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_launcher)
        .setAutoCancel(true)
        .setContentText("This msg won't display in your phone, only on wear you can see.")
        .setContentTitle("Hello")
    
        .setContentIntent(
                PendingIntent.getActivity(context, 10,intent,PendingIntent.FLAG_ONE_SHOT));
    
        NotificationManager mNM = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = mBuilder.build();
    
        RemoteViews contentView = new RemoteViews(context.getPackageName(),
        R.layout.notification_layout);
    
        contentView.setTextViewText(R.id.noti_text,"This message won't display in your wear device, only on phone you can see.");
        contentView.setImageViewResource(R.id.noti_image,R.drawable.ic_launcher);
        notification.contentView = contentView;
    
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
    
        mNM.notify(50, notification);
    

    Now run the app on your device and check the notification on both watch and phone, the content inside the RemoteViews won't display in your watch but on phone it will display and if you remove the .setContentTitle() & .setContentText(), then the RemoteViews content will be displayed on both watch and phone too.

提交回复
热议问题