Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

How to launch the Mobile app in click of Notification action button in wear watch android OS

I am new to android smart watch development so I need help how I can open the mobile app from wear watch notification action button.

Mobile app code to create the notification :-

Intent notificationIntent = new Intent(context, NotificationActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    notificationIntent.putExtra("ID", Id);
    notificationIntent.putExtra("Title", title);
    notificationIntent.setAction("NOTIFICATION");
   
    PendingIntent alertPendingIntent = PendingIntent.getActivity(context, Integer.parseInt(Id), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    //When the entire notification group is clicked
    Intent groupIntent = new Intent(context, NotificationActivity.class);
    groupIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    groupIntent.setAction("GroupNotification");
    PendingIntent groupPendingIntent = PendingIntent.getActivity(context, 0, groupIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    //Channel ID for the notification
    String NOTIFICATION_CHANNEL_ID = "12321312";
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  
    String GROUP_KEY = "com.example.notification.alerts.utils.sample";
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
                    .setSmallIcon(R.drawable.icon_notification)
                    .setColor(MyApplication.getInstance().getResources().getColor(R.color.app_icon_color))
                    .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true)
                    .setShowWhen(true)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(body))
                    .setGroup(GROUP_KEY)
                    .setPriority(Notification.PRIORITY_MAX);

    notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_notification_icon));

    PendingIntent pendingIntentSearch = searchActionClicked(context, notificationIntent);
            notificationBuilder.addAction(R.drawable.search, "Search", pendingIntentSearch );
            notificationBuilder.extend(new WearableExtender().addAction(createAction(R.drawable.search, "Search", pendingIntentSearch )));
            PendingIntent pendingIntent = openActionButtonClicked(context, notificationIntent);
            notificationBuilder.addAction(R.drawable.open, "open", pendingIntent);
            notificationBuilder.setContentIntent(alertPendingIntent);
            notificationBuilder.extend(new WearableExtender().addAction(createAction(R.drawable.open, "open", pendingIntent)));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Notification summaryNotification =
                new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
                        .setSmallIcon(R.drawable.small_icon_notification)
                        .setColor(MyApplication.getInstance().getResources().getColor(R.color.app_icon_color))
                        .setGroup(GROUP_KEY)
                        .setGroupSummary(true)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(groupPendingIntent)
                        .build();
        notificationManager.notify(0, summaryNotification);
    }

    notificationManager.notify(Integer.parseInt(Id), notificationBuilder.build());


private static PendingIntent searchCenterActionClicked(Context context, Intent notificationIntent) {
    Intent searchCenter = new Intent(context, NotificationActivity.class);
    searchCenter.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    searchCenter.putExtra("ID", Id);
    searchCenter.putExtra("Title", title);
    searchCenter.setAction("Search");
    return PendingIntent.getActivity(context, Integer.parseInt(Id), searchCenter, PendingIntent.FLAG_UPDATE_CURRENT);
}

private static PendingIntent openActionButtonClicked(Context context, Intent notificationIntent) {
    Intent open = new Intent(context, NotificationActivity.class);
    open.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    open.putExtra("ID", Id);
    open.putExtra("Title", title);
    open.setAction("open");
    return PendingIntent.getActivity(context, Integer.parseInt(Id), open, PendingIntent.FLAG_UPDATE_CURRENT);
}

I need help that automatically the notification will come to android smart watch once it is connected to phone

But how we will come to know that in smart watch any action button is been click so I can open the Mobile app in Phone when action button is click in watch sample as when user click in phone.

Simply I have to open the activity in mobile app in click of notification in smart watch.

Please help out what I have to do in smart watch code.

Thanks in advance


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...