Skip to main content
Samsung Developer Program

Example Code - Today's Weather

Example code for a weather information rich notification message

Today's Weather uses a large header primary section and a standard secondary section.

package com.samsung.android.richnotification.sample;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.text.format.Time;

import com.samsung.android.sdk.richnotification.SrnAction;
import com.samsung.android.sdk.richnotification.SrnAction.CallbackIntent;
import com.samsung.android.sdk.richnotification.SrnImageAsset;
import com.samsung.android.sdk.richnotification.SrnRichNotification;
import com.samsung.android.sdk.richnotification.SrnRichNotification.AlertType;
import com.samsung.android.sdk.richnotification.actions.SrnHostAction;
import com.samsung.android.sdk.richnotification.actions.SrnRemoteBuiltInAction;
import com.samsung.android.sdk.richnotification.actions.SrnRemoteInputAction;
import com.samsung.android.sdk.richnotification.actions.SrnRemoteInputAction.InputModeFactory;
import com.samsung.android.sdk.richnotification.actions.SrnRemoteInputAction.KeyboardInputMode;
import com.samsung.android.sdk.richnotification.actions.SrnRemoteInputAction.KeyboardInputMode
.KeyboardType;
import com.samsung.android.sdk.richnotification.actions.SrnRemoteInputAction
.SingleSelectInputMode;
import com.samsung.android.sdk.richnotification.templates.SrnLargeHeaderTemplate;
import com.samsung.android.sdk.richnotification.templates.SrnPrimaryTemplate;
import com.samsung.android.sdk.richnotification.templates.SrnSecondaryTemplate;
import com.samsung.android.sdk.richnotification.templates.SrnStandardSecondaryTemplate;

public class LargeHeaderExample implements IExample {

    private final Context mContext;

    public LargeHeaderExample(Context ctx) {
    mContext = ctx;
    }

    @Override
    public SrnRichNotification createRichNoti() {
        SrnRichNotification myRichNotification = new SrnRichNotification(mContext);
        myRichNotification.setReadout("Today's Weather", " 40 percent chance light rain");

        myRichNotification.setPrimaryTemplate(myPrimarySection());

        myRichNotification.setSecondaryTemplate(mySecondarySection());
        try{
        myRichNotification.addActionsWithPermissionCheck(actionList());
        } catch(Exception e)
            {
            e.printStackTrace(); 
            }
        myRichNotification.setAlertType(AlertType.SOUND_AND_VIBRATION,
                                                SrnRichNotification.PopupType.NORMAL);

        Bitmap notificationIconBitmap =
                                  BitmapFactory.decodeResource(mContext.getResources(),
                                  R.drawable.rain_icon);
        SrnImageAsset notificationIcon = new SrnImageAsset(mContext, "notification_icon",
                                                                  notificationIconBitmap); 
        
        myRichNotification.setIcon(notificationIcon):
        myRichNotification.setTitle("" +
                "Today's Weather");

        return myRichNotification;
    }

    public SrnPrimaryTemplate myPrimarySection() {
        SrnLargeHeaderTemplate largeHeaderTemplate = new SrnLargeHeaderTemplate();

        Bitmap bgBitmap = BitmapFactory.decodeResource(mContext.getResources(), 
                                                                  R.drawable.stormy_weather);
        SrnImageAsset bgHeaderImage = new SrnImageAsset(mContext, 
                                                           "background_header_image", bgBitmap);

        largeHeaderTemplate.setBackgroundImage(bgHeaderImage);

        return largeHeaderTemplate;
    }

    public SrnSecondaryTemplate mySecondarySection() {
        SrnStandardSecondaryTemplate standardSecondaryTemplate = new 
                                                           SrnStandardSecondaryTemplate();

        standardSecondaryTemplate.setTitle("<b>Weather Summary</b>");
        Time today = new Time(Time.getCurrentTimezone());
        today.setToNow();

        standardSecondaryTemplate.setSubHeader("<b>Today's Date</b>:" + today.year + "/" 
                                                    + today.month + "/" + today.monthDay);

        standardSecondaryTemplate.setBody("<b>40% Light Rain</b>: In the morning"
            + "a low of 56 degrees. In the early afternoon, mostly cloudy and warming "
            + "to 61 degrees. In the late afternoon, mostly cloudy with sunset at 5:20 pm. "
            + "Before midnight, clearing and cooling. After midnight, a low of 44.");

        standardSecondaryTemplate.setBackgroundColor(Color.rgb(0, 0, 127));

        Bitmap sectionImageBitmap = BitmapFactory.decodeResource(mContext.getResources(), 
                                                                   R.drawable.cloud_graphic);
        SrnImageAsset sectionImage = new SrnImageAsset(mContext, "section_image", 
                                                                          sectionImageBitmap);
        standardSecondaryTemplate.setImage(sectionImage);

        Bitmap icon1Bitmap = BitmapFactory.decodeResource(mContext.getResources(),
                                                                   R.drawable.star_gold_icon);
        SrnImageAsset icon1Image = new SrnImageAsset(mContext, "comment_icon", 
                                                                                 icon1Bitmap);
        standardSecondaryTemplate.setSmallIcon1(icon1Image, "4/5");

        Bitmap icon2Bitmap = BitmapFactory.decodeResource(mContext.getResources(), 
                                                                   R.drawable.thumbs_up_icon);
        SrnImageAsset icon2Image = new SrnImageAsset(mContext, "like_icon", icon2Bitmap);
        standardSecondaryTemplate.setSmallIcon2(icon2Image, "389+");

        return standardSecondaryTemplate;
    }

    public List<SrnAction> actionList() {
        ArrayList<SrnAction> myActions = new ArrayList<SrnAction>();

        SrnHostAction primaryAction = new SrnHostAction("See Online Forecast");
        Bitmap primaryActionBitmap = BitmapFactory.decodeResource(mContext.getResources(),
        R.drawable.microphone_icon);
        SrnImageAsset primaryActionIcon = new SrnImageAsset(mContext, 
                                                   "primary_action_icon", primaryActionBitmap);

       primaryAction.setIcon(primaryActionIcon);
       primaryAction.setToast("Check your phone_icon!");
 
       Intent resultIntent = new Intent(Intent.ACTION_VIEW);
       resultIntent.setData(Uri.parse("http://www.weather.com"));

       primaryAction.setCallbackIntent(CallbackIntent.getActivityCallback(resultIntent));
 
       myActions.add(primaryAction);

       SrnRemoteBuiltInAction remoteBuiltInAction = new SrnRemoteBuiltInAction("Call");

       remoteBuiltInAction.setType(SrnRemoteBuiltInAction.OperationType.CALL);
       remoteBuiltInAction.setData(Uri.fromParts("tel", "+821095557811", null));
 
       myActions.add(remoteBuiltInAction);

       SrnRemoteInputAction keyRemoteInputAction = new SrnRemoteInputAction("Comment");

       KeyboardInputMode keyboardInputMode = InputModeFactory.createKeyboardInputMode();
       keyboardInputMode.setPrefillString("@name");
       keyboardInputMode.setCharacterLimit(140);
       keyboardInputMode.setKeyboardType(KeyboardType.NORMAL);

       keyRemoteInputAction.setRequestedInputMode(keyboardInputMode);

       Intent keyboardIntent = new Intent(mContext, SrnRichNotification.class);
       keyRemoteInputAction.setCallbackIntent(CallbackIntent
                                                    .getActivityCallback(keyboardIntent));

       myActions.add(keyRemoteInputAction);

       SrnRemoteInputAction singleRemoteInputAction = new SrnRemoteInputAction("Forecast 
                                                                                        Survey");

       Bitmap iconSelect3Bitmap = BitmapFactory.decodeResource(mContext.getResources(),
                                                                      R.drawable.star_gold_icon);
       SrnImageAsset iconSelect3Image = new SrnImageAsset(mContext, "comment_icon", 
                                                                          iconSelect3Bitmap);

       SingleSelectInputMode singleInputMode = 
                                             InputModeFactory.createSingleSelectInputMode();
       singleInputMode.addChoice("Yes", "01", iconSelect3Image);
       singleInputMode.addChoice("No", "02", iconSelect3Image);
       singleInputMode.addChoice("Maybe", "03", iconSelect3Image);
       singleInputMode.addChoice("Don't know", "04", iconSelect3Image);

       singleRemoteInputAction.setRequestedInputMode(singleInputMode);
       singleRemoteInputAction.setDescription("Do you think the forecast will come 
                                                                                        true?");

       Intent singleSelectIntent = new Intent(mContext, SrnRichNotification.class);
       singleRemoteInputAction
                     .setCallbackIntent(CallbackIntent.getActivityCallback(singleSelectIntent));

       myActions.add(singleRemoteInputAction);

       SrnRemoteInputAction multiRemoteInputAction = new SrnRemoteInputAction("Favorite 
                                                                          Forecast Qualities");
       SrnRemoteInputAction.MultiSelectInputMode multiSelectInputMode = 
                                             InputModeFactory.createMultiSelectInputMode();

       multiRemoteInputAction.setDescription("Select the qualities of the forecast 
                                                                   service that you like.");
       multiSelectInputMode.addChoice("Accuracy", "02");
       multiSelectInputMode.addChoice("Preciseness", "04");
       multiSelectInputMode.addChoice("Up-to-the-minute", "06");
       multiSelectInputMode.addChoice("Localized", "08");

       multiRemoteInputAction.setRequestedInputMode(multiSelectInputMode);

       Intent multipleSelectIntent = new Intent(mContext, 
                                                            RichNotificationActivity.class);
       multiRemoteInputAction.setCallbackIntent(CallbackIntent
                                                .getActivityCallback(multipleSelectIntent));

       myActions.add(multiRemoteInputAction);



       return myActions;
    }
}

 

  • Was this article helpful?