3

I have an apple watch app that is not independent from the iPhone and I receive push notifications successfully on my watch.

However, tapping the push notification always opens the watch app. I do not want to handle this in the watch app. Is there a way to do this? Is there a way to not open the watch app on tap?

I've tried using the handleAction() functions in the WKExtensionDelegate but these are apparently now deprecated, and they never get fired.

Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98

1 Answers1

0

This might not be possible. It relies on the OS.

Possible reasons for this is notifications are for the user to click and open the app usually. And this might go against apple app guidelines too to prevent the app from opening.

However, you can try these:

This is an example of a foreground service with the notification.

public class UploadService extends IntentService{
 
    private NotificationCompat.Builder mBuilder;
 
    public UploadService() {
        super("UploadService");
    }
 
    @Override
    protected void onHandleIntent(Intent intent) {
        Intent deleteIntent = new Intent(this, CancelUploadReceiver.class);
        PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
 
        //building the notification
        mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(android.R.drawable.ic_menu_upload)
        .setContentTitle("Uploading Media...")
        .setTicker("Starting uploads")
        .addAction(android.R.drawable.ic_menu_close_clear_cancel, "Cancel Upload", pendingIntentCancel);
 
        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingIntent);
        mBuilder.setProgress(100, 0, true);
 
        startForeground(12345, mBuilder.build());
 
        for(int i=0;i<10;i++){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
 
    }
 
}

You need to register the CancelUploadReceiver in the manifest file.

<receiver android:name=".CancelUploadReceiver"/>

And when the “Cancel Upload” is tapped it will receive the broadcast. Then we can simply stop the service.

public class CancelUploadReceiver extends BroadcastReceiver{
 
@Override
 public void onReceive(Context context, Intent intent) {
   Intent service = new Intent();
   service.setComponent(new ComponentName(context,UploadService.class));
   context.stopService(service);
 }
 
}

Another option might be to create a Custom Interface that implements the UINotificationContentExtension protocol.

Related:

DialFrost
  • 1,610
  • 1
  • 8
  • 28