Last updated 27 October 2010. Created on 27 October 2010.
Edited by cdonner. Log in to edit this page.

I had the requirement that users need to control their forum subscription with a radio button control on their profile page. The label for the control says something like "notify me whenever there is a new topic posted on the forum".
Besides Content Profile, my solution uses Messaging & Notifications, and Rules. I created two rules that fire when a Content Profile node is created or updated, using standard Rules functionality. Both rules contain the same "Execute Custom PHP Code" action, and this is where the meat is. I found zero references for how to use the Notifications API with node type subscriptions and had to resort to trial and error, which is why I found it worthy of being posted here.

My profile node has a CCK field called field_notify. It either contains the string "Yes" or "No".

The code uses Rules substitution patterns and will not run outside of a Rules PHP action without changes.

// get all node type subscriptions for the user (author is the profile's author, i.e. the subscriber)
$subs = notifications_user_get_subscriptions($author->uid, 'node', 'type');

// determine if subscription exists
foreach ($subs as $key => $sub) {
  if ($sub->value  == 'forum') {
    $forumsubscription = $sub;

// requested, but does not exist: create
if ([node:field_notify-raw] == "Yes" && !$forumsubscription) {
  $subscription = array(
      'uid' => $author->uid,
      'type' => 'nodetype',
      'event_type' => 'node',
      'send_method' => 'mail',
      'send_interval' => '0',
      'fields' => array('type' => 'forum'),
  $result = notifications_save_subscription($subscription);    
  if (!($result === SAVED_NEW)) {
      drupal_set_message(t('Error: Your subscription was not created.'), 'error');
// not requested, but exists: delete
else if ([node:field_notify-raw] != "Yes" && $forumsubscription) {

Looking for support? Visit the forums, or join #drupal-support in IRC.