Update a notification subscription

Merchant API code sample to update a notification subscription.

Java

  // Copyright 2025 Google LLC 
 // 
 // Licensed under the Apache License, Version 2.0 (the "License"); 
 // you may not use this file except in compliance with the License. 
 // You may obtain a copy of the License at 
 // 
 //      https://www.apache.org/licenses/LICENSE-2.0 
 // 
 // Unless required by applicable law or agreed to in writing, software 
 // distributed under the License is distributed on an "AS IS" BASIS, 
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 // See the License for the specific language governing permissions and 
 // limitations under the License. 
 package 
  
 shopping.merchant.samples.notifications.v1 
 ; 
 import 
  
 com.google.api.gax.core.FixedCredentialsProvider 
 ; 
 import 
  
 com.google.auth.oauth2.GoogleCredentials 
 ; 
 import 
  
 com.google.protobuf.FieldMask 
 ; 
 import 
  
 com.google.shopping.merchant.notifications.v1.NotificationSubscription 
 ; 
 import 
  
 com.google.shopping.merchant.notifications.v1.NotificationSubscription.NotificationEventType 
 ; 
 import 
  
 com.google.shopping.merchant.notifications.v1.NotificationSubscriptionName 
 ; 
 import 
  
 com.google.shopping.merchant.notifications.v1.NotificationsApiServiceClient 
 ; 
 import 
  
 com.google.shopping.merchant.notifications.v1.NotificationsApiServiceSettings 
 ; 
 import 
  
 com.google.shopping.merchant.notifications.v1.UpdateNotificationSubscriptionRequest 
 ; 
 import 
  
 shopping.merchant.samples.utils.Authenticator 
 ; 
 import 
  
 shopping.merchant.samples.utils.Config 
 ; 
 /** 
 * This class demonstrates how to update a specific Notification Subscription for a given Merchant 
 * Center account. 
 */ 
 public 
  
 class 
 UpdateNotificationSubscriptionSample 
  
 { 
  
 public 
  
 static 
  
 void 
  
 updateNotificationSubscription 
 ( 
 Config 
  
 config 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 // Obtains OAuth token based on the user's configuration. 
  
 GoogleCredentials 
  
 credential 
  
 = 
  
 new 
  
 Authenticator 
 (). 
 authenticate 
 (); 
  
 // Creates service settings using the credentials retrieved above. 
  
 NotificationsApiServiceSettings 
  
 notificationsApiServiceSettings 
  
 = 
  
 NotificationsApiServiceSettings 
 . 
 newBuilder 
 () 
  
 . 
 setCredentialsProvider 
 ( 
 FixedCredentialsProvider 
 . 
 create 
 ( 
 credential 
 )) 
  
 . 
 build 
 (); 
  
 // Replace "YOUR_NOTIFICATION_SUBSCRIPTION_ID" with the actual ID. 
  
 String 
  
 notificationSubscriptionId 
  
 = 
  
 "YOUR_NOTIFICATION_SUBSCRIPTION_ID" 
 ; 
  
 // The event type to update the subscription to. 
  
 NotificationEventType 
  
 eventType 
  
 = 
  
 NotificationEventType 
 . 
 PRODUCT_STATUS_CHANGE 
 ; 
  
 // The new callback URI to update the subscription to. 
  
 String 
  
 callbackUri 
  
 = 
  
 "https://an-updated-uri.com" 
 ; 
  
 // Set to true to update the subscription to apply to all managed accounts. 
  
 boolean 
  
 allManagedAccounts 
  
 = 
  
 true 
 ; 
  
 // Creates notification subscription name to identify the subscription. 
  
 NotificationSubscriptionName 
  
 subscriptionName 
  
 = 
  
 NotificationSubscriptionName 
 . 
 newBuilder 
 () 
  
 . 
 setAccount 
 ( 
 config 
 . 
 getAccountId 
 (). 
 toString 
 ()) 
  
 . 
 setNotificationSubscription 
 ( 
 notificationSubscriptionId 
 ) 
  
 . 
 build 
 (); 
  
 // Calls the API and catches and prints any network failures/errors. 
  
 try 
  
 ( 
 NotificationsApiServiceClient 
  
 notificationSubscriptionServiceClient 
  
 = 
  
 NotificationsApiServiceClient 
 . 
 create 
 ( 
 notificationsApiServiceSettings 
 )) 
  
 { 
  
 // Create the updated NotificationSubscription object. 
  
 NotificationSubscription 
  
 updatedSubscription 
  
 = 
  
 NotificationSubscription 
 . 
 newBuilder 
 () 
  
 . 
 setName 
 ( 
 subscriptionName 
 . 
 toString 
 ()) 
  
 . 
 setCallBackUri 
 ( 
 callbackUri 
 ) 
  
 . 
 setRegisteredEvent 
 ( 
 eventType 
 ) 
  
 . 
 setAllManagedAccounts 
 ( 
 allManagedAccounts 
 ) 
  
 . 
 build 
 (); 
  
 // Create a FieldMask to specify which fields to update.  This is required for updates. 
  
 FieldMask 
  
 updateMask 
  
 = 
  
 FieldMask 
 . 
 newBuilder 
 () 
  
 . 
 addPaths 
 ( 
 "call_back_uri" 
 ) 
  
 . 
 addPaths 
 ( 
 "registered_event" 
 ) 
  
 . 
 addPaths 
 ( 
 "all_managed_accounts" 
 ) 
  
 . 
 build 
 (); 
  
 // Create the update request. 
  
 UpdateNotificationSubscriptionRequest 
  
 request 
  
 = 
  
 UpdateNotificationSubscriptionRequest 
 . 
 newBuilder 
 () 
  
 . 
 setNotificationSubscription 
 ( 
 updatedSubscription 
 ) 
  
 . 
 setUpdateMask 
 ( 
 updateMask 
 ) 
  
 . 
 build 
 (); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Sending update Notification Subscription request:" 
 ); 
  
 NotificationSubscription 
  
 response 
  
 = 
  
 notificationSubscriptionServiceClient 
 . 
 updateNotificationSubscription 
 ( 
 request 
 ); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Updated Notification Subscription below:" 
 ); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 response 
 ); 
  
 } 
  
 catch 
  
 ( 
 Exception 
  
 e 
 ) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 e 
 ); 
  
 } 
  
 } 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 Config 
  
 config 
  
 = 
  
 Config 
 . 
 load 
 (); 
  
 updateNotificationSubscription 
 ( 
 config 
 ); 
  
 } 
 } 
  
 

PHP

 < ?php 
 /** 
 * Copyright 2025 Google LLC 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *     https://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */ 
 require_once __DIR__ . '/../../../vendor/autoload.php'; 
 require_once __DIR__ . '/../../Authentication/Authentication.php'; 
 require_once __DIR__ . '/../../Authentication/Config.php'; 
 use Google\ApiCore\ApiException; 
 use Google\Protobuf\FieldMask; 
 use Google\Shopping\Merchant\Notifications\V1\Client\NotificationsApiServiceClient; 
 use Google\Shopping\Merchant\Notifications\V1\NotificationSubscription; 
 use Google\Shopping\Merchant\Notifications\V1\NotificationSubscription\NotificationEventType; 
 use Google\Shopping\Merchant\Notifications\V1\UpdateNotificationSubscriptionRequest; 
 /** 
 * Updates a Notification Subscription. 
 */ 
 class UpdateNotificationSubscription 
 { 
 /** 
 * Updates a specific Notification Subscription for a given Merchant Center account. 
 * 
 * @param array $config 
 *  Configuration data for authentication and account details. 
 * @param string $notificationSubscriptionId 
 *  The ID of the notification subscription to update. 
 * @param int $eventType 
 *  The new event type for the subscription. 
 * @param string $callbackUri 
 *  The new callback URI for the subscription. 
 * @param bool $allManagedAccounts 
 *  Whether to apply the subscription to all managed accounts. 
 * @throws ApiException 
 */ 
 public static function updateNotificationSubscriptionSample( 
 $config, 
 $notificationSubscriptionId, 
 $eventType, 
 $callbackUri, 
 $allManagedAccounts 
 ): void { 
 // Get OAuth credentials. 
 $credentials = Authentication::useServiceAccountOrTokenFile(); 
 // Set up client options. 
 $options = ['credentials' => $credentials]; 
 // Create a client instance. 
 $notificationsApiServiceClient = new NotificationsApiServiceClient($options); 
 // Construct the resource name. 
 $subscriptionName = "accounts/" . $config['accountId'] . 
 "/notificationsubscriptions/" . $notificationSubscriptionId; 
 // Create the updated NotificationSubscription object. 
 $updatedSubscription = new NotificationSubscription([ 
 'name' => $subscriptionName, 
 'call_back_uri' => $callbackUri, 
 'registered_event' => $eventType, 
 'all_managed_accounts' => $allManagedAccounts, 
 ]); 
 // Create a FieldMask to specify which fields to update. 
 $updateMask = new FieldMask([ 
 'paths' => ['call_back_uri', 'registered_event', 'all_managed_accounts'], 
 ]); 
 // Create the update request. 
 $request = new UpdateNotificationSubscriptionRequest([ 
 'notification_subscription' => $updatedSubscription, 
 'update_mask' => $updateMask, 
 ]); 
 print "Sending update Notification Subscription request:\n"; 
 // Make the API call. 
 try { 
 $response = $notificationsApiServiceClient->updateNotificationSubscription($request); 
 print "Updated Notification Subscription below:\n"; 
 print_r($response); 
 } catch (ApiException $e) { 
 print "Request failed:\n"; 
 print $e->getMessage() . "\n"; 
 } 
 } 
 /** 
 * Execute the sample. 
 * 
 * @throws ApiException 
 */ 
 public function callSample(): void 
 { 
 $config = Config::generateConfig(); 
 // Replace with the actual notification subscription ID. 
 $notificationSubscriptionId = "YOUR_NOTIFICATION_SUBSCRIPTION_ID"; 
 // Set the new event type. 
 $eventType = NotificationEventType::PRODUCT_STATUS_CHANGE; 
 // Set the new callback URI. 
 $callbackUri = "https://an-updated-uri.com"; 
 // Set whether to apply to all managed accounts. 
 $allManagedAccounts = true; 
 self::updateNotificationSubscriptionSample( 
 $config, 
 $notificationSubscriptionId, 
 $eventType, 
 $callbackUri, 
 $allManagedAccounts 
 ); 
 } 
 } 
 // Run the script. 
 $sample = new UpdateNotificationSubscription(); 
 $sample->callSample(); 
  
 

Python

  # -*- coding: utf-8 -*- 
 # Copyright 2025 Google LLC 
 # 
 # Licensed under the Apache License, Version 2.0 (the "License"); 
 # you may not use this file except in compliance with the License. 
 # You may obtain a copy of the License at 
 # 
 #     http://www.apache.org/licenses/LICENSE-2.0 
 # 
 # Unless required by applicable law or agreed to in writing, software 
 # distributed under the License is distributed on an "AS IS" BASIS, 
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 # See the License for the specific language governing permissions and 
 # limitations under the License. 
 """A module to update NotificationSubscription.""" 
 from 
  
 examples.authentication 
  
 import 
 configuration 
 from 
  
 examples.authentication 
  
 import 
 generate_user_credentials 
 from 
  
 google.protobuf 
  
 import 
 field_mask_pb2 
 from 
  
 google.shopping.merchant_notifications_v1 
  
 import 
 NotificationsApiServiceClient 
 from 
  
 google.shopping.merchant_notifications_v1 
  
 import 
 NotificationSubscription 
 from 
  
 google.shopping.merchant_notifications_v1 
  
 import 
 UpdateNotificationSubscriptionRequest 
 _ACCOUNT 
 = 
 configuration 
 . 
 Configuration 
 () 
 . 
 read_merchant_info 
 () 
 def 
  
 update_notification_subscription 
 (): 
  
 """Updates a specific Notification Subscription for a given Merchant Center account.""" 
 # Gets OAuth Credentials. 
 credentials 
 = 
 generate_user_credentials 
 . 
 main 
 () 
 # Creates a client. 
 client 
 = 
 NotificationsApiServiceClient 
 ( 
 credentials 
 = 
 credentials 
 ) 
 # Replace "YOUR_NOTIFICATION_SUBSCRIPTION_ID" with the actual ID. 
 notification_subscription_id 
 = 
 "YOUR_NOTIFICATION_SUBSCRIPTION_ID" 
 # The event type to update the subscription to. 
 event_type 
 = 
 ( 
 NotificationSubscription 
 . 
 NotificationEventType 
 . 
 PRODUCT_STATUS_CHANGE 
 ) 
 # The new callback URI to update the subscription to. 
 callback_uri 
 = 
 "https://an-updated-uri.com" 
 # Set to true to update the subscription to apply to all managed accounts. 
 all_managed_accounts 
 = 
 True 
 # Creates notification subscription name to identify the subscription. 
 subscription_name 
 = 
 f 
 "accounts/ 
 { 
 _ACCOUNT 
 } 
 /notificationsubscriptions/ 
 { 
 notification_subscription_id 
 } 
 " 
 # Creates the updated NotificationSubscription object. 
 updated_subscription 
 = 
 NotificationSubscription 
 () 
 updated_subscription 
 . 
 name 
 = 
 subscription_name 
 updated_subscription 
 . 
 call_back_uri 
 = 
 callback_uri 
 updated_subscription 
 . 
 registered_event 
 = 
 event_type 
 updated_subscription 
 . 
 all_managed_accounts 
 = 
 all_managed_accounts 
 # Creates a FieldMask to specify which fields to update. 
 update_mask 
 = 
 field_mask_pb2 
 . 
 FieldMask 
 ( 
 paths 
 = 
 [ 
 "call_back_uri" 
 , 
 "registered_event" 
 , 
 "all_managed_accounts" 
 ] 
 ) 
 # Creates the update request. 
 request 
 = 
 UpdateNotificationSubscriptionRequest 
 ( 
 notification_subscription 
 = 
 updated_subscription 
 , 
 update_mask 
 = 
 update_mask 
 ) 
 print 
 ( 
 "Sending update Notification Subscription request:" 
 ) 
 # Makes the request and catches and prints any error messages. 
 try 
 : 
 response 
 = 
 client 
 . 
 update_notification_subscription 
 ( 
 request 
 = 
 request 
 ) 
 print 
 ( 
 "Updated Notification Subscription below:" 
 ) 
 print 
 ( 
 response 
 ) 
 except 
 RuntimeError 
 as 
 e 
 : 
 print 
 ( 
 e 
 ) 
 if 
 __name__ 
 == 
 "__main__" 
 : 
 update_notification_subscription 
 () 
  
 
Design a Mobile Site
View Site in Mobile | Classic
Share by: