SlideShare a Scribd company logo
1 of 10
Android - How To Create Push Notifications With 
Custom View? 
Today, most Android applications are integrated with the option to send push notifications. 
Developers and app publishers consider this capability as one of the most important actions in 
maintaining the relationship with their users and the ability to motivate them into performing 
certain actions within the app. However, in most applications the display of push notification 
message is quite basic - a miniature version of the app icon, a title (most of the time it will be the 
app name), with a short description below it. 
Push notification message can be much more interesting! One of the better examples for it, is 
the way in which Groupon send their push messages - big and seductive picture, with important 
details such as price and the amount of discount. They also display action buttons!
In this tutorial we will learn how to create a rich push notification message. 
Integrating Push Notifications in your app 
If your app doesn’t yet support in basic push notification, please refer to PushApps short tutorial: 
https://wiki.pushapps.mobi/display/PUSHAPPS/Android+Getting+Started 
This tutorial assumes you have completed the basic push notifications integration, and you are 
able to receive notifications to your device. We will take you step by step from this phase and 
show you how to code and design the notification. 
Push Notification received event 
After you register the device to PushApps with your private keys, we would like to “take control” 
over the push notification received event. We would like to perform certain actions and display 
our custom view. With PushApps it’s easy: 
1. In you Application class (if you don’t have one, please create it) register to PushApps with 
your Google API Project Number and PushApps Token.
@Override 
public void onCreate() { 
super.onCreate(); 
// first we initialize the push manager, you can also initialize the 
// PushManager in your main activity. 
PushManager.init(getBaseContext(), GOOGLE_API_PROJECT_NUMBER, 
PUSHAPPS_APP_TOKEN); 
PushManager.getInstance(getApplicationContext()) 
.setShouldStartIntentAsNewTask(false); 
// these methods are both optional and used for the notification 
// customization 
PushManager.getInstance(getApplicationContext()).setShouldStackNotifications(true); 
} 
2. We want PushApps to notify us when a new push notification received to the device. For that, 
we need to implement the PushAppsMessageInterface. 
@Override 
public void onCreate() { 
super.onCreate(); 
// first we initialize the push manager, you can also initi alize the 
// PushManager in your main activity. 
PushManager.init(getBaseContext(), GOOGLE_API_PROJECT_NUMBER, PUSHAPPS_APP_TOKEN); 
PushManager.getInstance(getApplicationContext()).setShouldStartIntentAsNewTask(false); 
// these methods are both optional and used for the notification 
// customization 
PushManager.getInstance(getApplicationContext()).setShouldStackNotifications( true); 
// register for message events 
PushManager.getInstance(getApplicationContext()).registerForMessagesEvents( 
new PushAppsMessageInterface() { 
// This method will get called every time the device will receive a push message 
@Override 
public void onMessage(Context ctx, Intent intent) {
Log.d(“PushAppsDemo”, “We got a message!”); 
} 
} 
Creating the custom view 
The first step in building your own unique View, is writing the XML. There is nothing new in this 
section - exactly as we build a new view for an Activity or a Fragment, same is here. Like all 
other XMLs, this one will also should be placed inside the res / layout directory. In this tutorial, 
we’re creating a similar view as the one as Groupon. However, you can create any view you 
want, while the only limit is the view height - remember that this is not a full-screen view (but 
only shown in the Notification Center). 
1. Creating the XML: 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="250dp" 
android:orientation="vertical" 
android:background="@android:color/black"> 
<RelativeLayout 
android:id="@+id/notification_upper_layout" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
<ImageView 
android:id="@+id/notification_icon" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/small_pic"/> 
<TextView 
android:id="@+id/notification_title" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:text="PushApps Custom Notification" 
android:textColor="@android:color/white" 
android:textStyle="bold" 
android:textSize="16dp" 
android:layout_marginLeft="8dp" 
android:layout_toRightOf="@id/notification_icon"/> 
<TextView 
android:id="@+id/notification_subtitle" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Happy Holidays from PushApps! This is a special gift!" 
android:textColor="@android:color/white" 
android:textSize="14dp" 
android:layout_marginLeft="8dp" 
android:layout_toRightOf="@id/notification_icon" 
android:layout_below="@id/notification_title"/> 
</RelativeLayout> 
<RelativeLayout 
android:id="@+id/notification_main_layout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<ImageView 
android:id="@+id/notification_main_image_view" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
/> 
<RelativeLayout 
android:id="@+id/notification_bottom_layout" 
android:layout_width="fill_parent" 
android:layout_height="40dp" 
android:layout_alignParentBottom="true" 
android:layout_centerHorizontal="true" 
android:background="@android:color/black" 
android:alpha="0.2"> 
</RelativeLayout> 
<TextView
android:id="@+id/notification_colored_text" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:textColor="#F4F420" 
android:textSize="14dp" 
android:layout_alignParentBottom="true" 
android:padding="6dp" 
android:layout_marginLeft="5dp" 
android:background="@drawable/button_border" 
android:textStyle="bold"/> 
</RelativeLayout> 
</LinearLayout> 
2. We would like to link our new XML with our code, and of course update the view values (such 
as text and image) with those we received in the push notification JSON: 
// register for message events 
PushManager.getInstance(getApplicationContext()).registerForMessagesEvents(new 
PushAppsMessageInterface() { 
// This method will get called every time the device will receive a push message 
@Override 
public void onMessage(Context ctx, Intent intent) { 
// Get the title string from the push notification message 
String titleTxt = intent.getStringExtra(PushManager.NOTIFICATION_TITLE_KEY); 
// Get the message string from the push notification message 
String subTitleTxt = intent.getStringExtra(PushManager.NOTIFICATION_MESSAGE_KEY); 
String extraData = intent.getStringExtra("info"); // Your Custom JSON key 
String actionButton = "Click Me!"; // Some default string 
String imageUrl = ""; 
try { 
JSONObject jsonObject = new JSONObject(extraData); 
// Extract the text for our action button, from the custom JSON 
actionButton = jsonObject.getString("button_text"); 
imageUrl = jsonObject.getString("picture_url");
} catch (JSONException e) {} 
// The intent to start, when the user clicks the notification 
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class); 
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext()); 
stackBuilder.addParentStack(MainActivity.class); 
stackBuilder.addNextIntent(resultIntent); 
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 
0, PendingIntent.FLAG_UPDATE_CURRENT); 
// The custom view 
RemoteViews expandedView = new RemoteViews( 
getApplicationContext().getPackageName(),R.layout.custom_notification); 
expandedView.setTextViewText(R.id.notification_title, titleTxt); 
expandedView.setTextViewText(R.id.notification_subtitle, subTitleTxt); 
expandedView.setTextViewText(R.id.notification_colored_text, actionButton); 
expandedView.setImageViewBitmap( 
R.id.notification_main_image_view, drawableFromUrl(imageUrl)); 
// Building the notification that will show up in the notification center 
Notification notification = new NotificationCompat.Builder( 
getApplicationContext()) 
.setSmallIcon(R.drawable.small_pic) 
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI) 
.setDefaults(Notification.DEFAULT_VIBRATE) 
.setAutoCancel(true) 
.setContentIntent(resultPendingIntent) 
.setContentTitle(titleTxt) 
.setContentText(subTitleTxt) 
.build(); 
notification.bigContentView = expandedView; 
NotificationManager mNotificationManager = (NotificationManager) 
getSystemService(Context.NOTIFICATION_SERVICE); 
mNotificationManager.notify(1, notification); 
} 
});
Sending a Push Notification from the PushApps 
Admin Console 
Sending a push notification from the PushApps Admin Console is very simple. You can learn a 
lot about it from our WIKI - http://wiki.pushapps.mobi/display/PUSHAPPS/Admin+Console. 
Please notice that the parameters keys that you provide in the Admin Console should match 
those in your Android code. 
Finally!
Some minor side notes: 
* Notice that this code is compatible for Android devices running API 11 version or higher. There 
are similar solutions for custom push notifications view for older devices - just google it. 
* If you Notification Center in your device is full with other push notifications from other apps, the 
push notification view will appear in its small and regular view (without the custom view). That’s 
why it’s important to provide the parameters for the regular state (e.g. Small Icon, Content Title, 
Content Text).
Checkout the entire source code from here. 
Happy Coding!

More Related Content

What's hot

Introduction to Android Wear
Introduction to Android WearIntroduction to Android Wear
Introduction to Android WearPeter Friese
 
Android Wear Presentation
Android Wear PresentationAndroid Wear Presentation
Android Wear PresentationZi Yong Chua
 
Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014
Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014
Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014Gil Irizarry
 
android level 3
android level 3android level 3
android level 3DevMix
 
4.preference management
4.preference management 4.preference management
4.preference management maamir farooq
 
See Androids Fighting: Connect Salesforce with Your Android Wear Watch
See Androids Fighting: Connect Salesforce with Your Android Wear WatchSee Androids Fighting: Connect Salesforce with Your Android Wear Watch
See Androids Fighting: Connect Salesforce with Your Android Wear WatchSalesforce Developers
 
Developing for Android Wear
Developing for Android WearDeveloping for Android Wear
Developing for Android WearCan Elmas
 
How to setup Author Advertising Plugin( full guide)
How to setup Author Advertising Plugin( full guide)How to setup Author Advertising Plugin( full guide)
How to setup Author Advertising Plugin( full guide)richard fernandes
 
I/O Rewind 2015 : Android Design Support Library
I/O Rewind 2015 : Android Design Support LibraryI/O Rewind 2015 : Android Design Support Library
I/O Rewind 2015 : Android Design Support LibrarySittiphol Phanvilai
 
Android Design Support Library
Android Design Support LibraryAndroid Design Support Library
Android Design Support LibraryIbnu Sina Wardy
 
Support Design Library
Support Design LibrarySupport Design Library
Support Design LibraryTaeho Kim
 
Android TV: Building apps with Google’s Leanback Library
Android TV: Building apps with  Google’s Leanback LibraryAndroid TV: Building apps with  Google’s Leanback Library
Android TV: Building apps with Google’s Leanback LibraryJoe Birch
 
Android tv get started
Android tv get startedAndroid tv get started
Android tv get startedAscii Huang
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycleAhsanul Karim
 
UI Testing for Your Xamarin.Forms Apps
UI Testing for Your Xamarin.Forms AppsUI Testing for Your Xamarin.Forms Apps
UI Testing for Your Xamarin.Forms AppsCodrina Merigo
 
Langkah-langkah Instalasi software untuk develop aplikasi android
Langkah-langkah Instalasi software untuk develop aplikasi androidLangkah-langkah Instalasi software untuk develop aplikasi android
Langkah-langkah Instalasi software untuk develop aplikasi androidAgus Haryanto
 
Android accessibility for developers and QA
Android accessibility for developers and QAAndroid accessibility for developers and QA
Android accessibility for developers and QATed Drake
 

What's hot (20)

Introduction to Android Wear
Introduction to Android WearIntroduction to Android Wear
Introduction to Android Wear
 
Android Wear Presentation
Android Wear PresentationAndroid Wear Presentation
Android Wear Presentation
 
Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014
Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014
Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014
 
android level 3
android level 3android level 3
android level 3
 
4.preference management
4.preference management 4.preference management
4.preference management
 
Android Wear Development
Android Wear DevelopmentAndroid Wear Development
Android Wear Development
 
Using android's action bar
Using android's action barUsing android's action bar
Using android's action bar
 
See Androids Fighting: Connect Salesforce with Your Android Wear Watch
See Androids Fighting: Connect Salesforce with Your Android Wear WatchSee Androids Fighting: Connect Salesforce with Your Android Wear Watch
See Androids Fighting: Connect Salesforce with Your Android Wear Watch
 
Developing for Android Wear
Developing for Android WearDeveloping for Android Wear
Developing for Android Wear
 
How to setup Author Advertising Plugin( full guide)
How to setup Author Advertising Plugin( full guide)How to setup Author Advertising Plugin( full guide)
How to setup Author Advertising Plugin( full guide)
 
I/O Rewind 2015 : Android Design Support Library
I/O Rewind 2015 : Android Design Support LibraryI/O Rewind 2015 : Android Design Support Library
I/O Rewind 2015 : Android Design Support Library
 
Android Design Support Library
Android Design Support LibraryAndroid Design Support Library
Android Design Support Library
 
Support Design Library
Support Design LibrarySupport Design Library
Support Design Library
 
Android TV: Building apps with Google’s Leanback Library
Android TV: Building apps with  Google’s Leanback LibraryAndroid TV: Building apps with  Google’s Leanback Library
Android TV: Building apps with Google’s Leanback Library
 
Layout
LayoutLayout
Layout
 
Android tv get started
Android tv get startedAndroid tv get started
Android tv get started
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycle
 
UI Testing for Your Xamarin.Forms Apps
UI Testing for Your Xamarin.Forms AppsUI Testing for Your Xamarin.Forms Apps
UI Testing for Your Xamarin.Forms Apps
 
Langkah-langkah Instalasi software untuk develop aplikasi android
Langkah-langkah Instalasi software untuk develop aplikasi androidLangkah-langkah Instalasi software untuk develop aplikasi android
Langkah-langkah Instalasi software untuk develop aplikasi android
 
Android accessibility for developers and QA
Android accessibility for developers and QAAndroid accessibility for developers and QA
Android accessibility for developers and QA
 

Similar to How to create android push notifications with custom view

Android Wearables ii
Android Wearables iiAndroid Wearables ii
Android Wearables iiKetan Raval
 
Implementation of Push Notification in React Native Android app using Firebas...
Implementation of Push Notification in React Native Android app using Firebas...Implementation of Push Notification in React Native Android app using Firebas...
Implementation of Push Notification in React Native Android app using Firebas...naseeb20
 
Push Notifications: How to add them to a Flutter App
Push Notifications: How to add them to a Flutter AppPush Notifications: How to add them to a Flutter App
Push Notifications: How to add them to a Flutter AppFibonalabs
 
Mobile 2.0 Open Ideas WorkShop: Building Social Media Enabled Apps on Android
Mobile 2.0 Open Ideas WorkShop: Building Social Media Enabled Apps on AndroidMobile 2.0 Open Ideas WorkShop: Building Social Media Enabled Apps on Android
Mobile 2.0 Open Ideas WorkShop: Building Social Media Enabled Apps on AndroidAlberto Ruibal
 
Session #8 adding magic to your app
Session #8  adding magic to your appSession #8  adding magic to your app
Session #8 adding magic to your appVitali Pekelis
 
Local Notification Tutorial
Local Notification TutorialLocal Notification Tutorial
Local Notification TutorialKetan Raval
 
Leture5 exercise onactivities
Leture5 exercise onactivitiesLeture5 exercise onactivities
Leture5 exercise onactivitiesmaamir farooq
 
Lecture exercise on activities
Lecture exercise on activitiesLecture exercise on activities
Lecture exercise on activitiesmaamir farooq
 
GDG GeorgeTown Devfest 2014 Presentation: Android Wear: A Developer's Perspec...
GDG GeorgeTown Devfest 2014 Presentation: Android Wear: A Developer's Perspec...GDG GeorgeTown Devfest 2014 Presentation: Android Wear: A Developer's Perspec...
GDG GeorgeTown Devfest 2014 Presentation: Android Wear: A Developer's Perspec...mharkus
 
Create an other activity lesson 3
Create an other activity lesson 3Create an other activity lesson 3
Create an other activity lesson 3Kalluri Vinay Reddy
 
Android Quiz App – Test Your IQ.pdf
Android Quiz App – Test Your IQ.pdfAndroid Quiz App – Test Your IQ.pdf
Android Quiz App – Test Your IQ.pdfSudhanshiBakre1
 
Unit5 Mobile Application Development.doc
Unit5 Mobile Application Development.docUnit5 Mobile Application Development.doc
Unit5 Mobile Application Development.docKNANTHINIMCA
 
Exercises broadcast receiver,incoming phone call
Exercises broadcast receiver,incoming phone callExercises broadcast receiver,incoming phone call
Exercises broadcast receiver,incoming phone callmaamir farooq
 
04 activities - Android
04   activities - Android04   activities - Android
04 activities - AndroidWingston
 

Similar to How to create android push notifications with custom view (20)

Android action bar and notifications-chapter16
Android action bar and notifications-chapter16Android action bar and notifications-chapter16
Android action bar and notifications-chapter16
 
Android Wearables ii
Android Wearables iiAndroid Wearables ii
Android Wearables ii
 
Implementation of Push Notification in React Native Android app using Firebas...
Implementation of Push Notification in React Native Android app using Firebas...Implementation of Push Notification in React Native Android app using Firebas...
Implementation of Push Notification in React Native Android app using Firebas...
 
Push Notifications: How to add them to a Flutter App
Push Notifications: How to add them to a Flutter AppPush Notifications: How to add them to a Flutter App
Push Notifications: How to add them to a Flutter App
 
Mobile 2.0 Open Ideas WorkShop: Building Social Media Enabled Apps on Android
Mobile 2.0 Open Ideas WorkShop: Building Social Media Enabled Apps on AndroidMobile 2.0 Open Ideas WorkShop: Building Social Media Enabled Apps on Android
Mobile 2.0 Open Ideas WorkShop: Building Social Media Enabled Apps on Android
 
Session #8 adding magic to your app
Session #8  adding magic to your appSession #8  adding magic to your app
Session #8 adding magic to your app
 
Local Notification Tutorial
Local Notification TutorialLocal Notification Tutorial
Local Notification Tutorial
 
Exercises
ExercisesExercises
Exercises
 
Create New Android Activity
Create New Android ActivityCreate New Android Activity
Create New Android Activity
 
Leture5 exercise onactivities
Leture5 exercise onactivitiesLeture5 exercise onactivities
Leture5 exercise onactivities
 
Lecture exercise on activities
Lecture exercise on activitiesLecture exercise on activities
Lecture exercise on activities
 
GDG GeorgeTown Devfest 2014 Presentation: Android Wear: A Developer's Perspec...
GDG GeorgeTown Devfest 2014 Presentation: Android Wear: A Developer's Perspec...GDG GeorgeTown Devfest 2014 Presentation: Android Wear: A Developer's Perspec...
GDG GeorgeTown Devfest 2014 Presentation: Android Wear: A Developer's Perspec...
 
Create an other activity lesson 3
Create an other activity lesson 3Create an other activity lesson 3
Create an other activity lesson 3
 
Android 3
Android 3Android 3
Android 3
 
Android Quiz App – Test Your IQ.pdf
Android Quiz App – Test Your IQ.pdfAndroid Quiz App – Test Your IQ.pdf
Android Quiz App – Test Your IQ.pdf
 
Unit5 Mobile Application Development.doc
Unit5 Mobile Application Development.docUnit5 Mobile Application Development.doc
Unit5 Mobile Application Development.doc
 
Exercises broadcast receiver,incoming phone call
Exercises broadcast receiver,incoming phone callExercises broadcast receiver,incoming phone call
Exercises broadcast receiver,incoming phone call
 
Android Basic Components
Android Basic ComponentsAndroid Basic Components
Android Basic Components
 
Notification android
Notification androidNotification android
Notification android
 
04 activities - Android
04   activities - Android04   activities - Android
04 activities - Android
 

Recently uploaded

VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...masabamasaba
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationShrmpro
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 

Recently uploaded (20)

VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 

How to create android push notifications with custom view

  • 1. Android - How To Create Push Notifications With Custom View? Today, most Android applications are integrated with the option to send push notifications. Developers and app publishers consider this capability as one of the most important actions in maintaining the relationship with their users and the ability to motivate them into performing certain actions within the app. However, in most applications the display of push notification message is quite basic - a miniature version of the app icon, a title (most of the time it will be the app name), with a short description below it. Push notification message can be much more interesting! One of the better examples for it, is the way in which Groupon send their push messages - big and seductive picture, with important details such as price and the amount of discount. They also display action buttons!
  • 2. In this tutorial we will learn how to create a rich push notification message. Integrating Push Notifications in your app If your app doesn’t yet support in basic push notification, please refer to PushApps short tutorial: https://wiki.pushapps.mobi/display/PUSHAPPS/Android+Getting+Started This tutorial assumes you have completed the basic push notifications integration, and you are able to receive notifications to your device. We will take you step by step from this phase and show you how to code and design the notification. Push Notification received event After you register the device to PushApps with your private keys, we would like to “take control” over the push notification received event. We would like to perform certain actions and display our custom view. With PushApps it’s easy: 1. In you Application class (if you don’t have one, please create it) register to PushApps with your Google API Project Number and PushApps Token.
  • 3. @Override public void onCreate() { super.onCreate(); // first we initialize the push manager, you can also initialize the // PushManager in your main activity. PushManager.init(getBaseContext(), GOOGLE_API_PROJECT_NUMBER, PUSHAPPS_APP_TOKEN); PushManager.getInstance(getApplicationContext()) .setShouldStartIntentAsNewTask(false); // these methods are both optional and used for the notification // customization PushManager.getInstance(getApplicationContext()).setShouldStackNotifications(true); } 2. We want PushApps to notify us when a new push notification received to the device. For that, we need to implement the PushAppsMessageInterface. @Override public void onCreate() { super.onCreate(); // first we initialize the push manager, you can also initi alize the // PushManager in your main activity. PushManager.init(getBaseContext(), GOOGLE_API_PROJECT_NUMBER, PUSHAPPS_APP_TOKEN); PushManager.getInstance(getApplicationContext()).setShouldStartIntentAsNewTask(false); // these methods are both optional and used for the notification // customization PushManager.getInstance(getApplicationContext()).setShouldStackNotifications( true); // register for message events PushManager.getInstance(getApplicationContext()).registerForMessagesEvents( new PushAppsMessageInterface() { // This method will get called every time the device will receive a push message @Override public void onMessage(Context ctx, Intent intent) {
  • 4. Log.d(“PushAppsDemo”, “We got a message!”); } } Creating the custom view The first step in building your own unique View, is writing the XML. There is nothing new in this section - exactly as we build a new view for an Activity or a Fragment, same is here. Like all other XMLs, this one will also should be placed inside the res / layout directory. In this tutorial, we’re creating a similar view as the one as Groupon. However, you can create any view you want, while the only limit is the view height - remember that this is not a full-screen view (but only shown in the Notification Center). 1. Creating the XML: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="250dp" android:orientation="vertical" android:background="@android:color/black"> <RelativeLayout android:id="@+id/notification_upper_layout" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/notification_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/small_pic"/> <TextView android:id="@+id/notification_title" android:layout_width="wrap_content" android:layout_height="wrap_content"
  • 5. android:text="PushApps Custom Notification" android:textColor="@android:color/white" android:textStyle="bold" android:textSize="16dp" android:layout_marginLeft="8dp" android:layout_toRightOf="@id/notification_icon"/> <TextView android:id="@+id/notification_subtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Happy Holidays from PushApps! This is a special gift!" android:textColor="@android:color/white" android:textSize="14dp" android:layout_marginLeft="8dp" android:layout_toRightOf="@id/notification_icon" android:layout_below="@id/notification_title"/> </RelativeLayout> <RelativeLayout android:id="@+id/notification_main_layout" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/notification_main_image_view" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <RelativeLayout android:id="@+id/notification_bottom_layout" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:background="@android:color/black" android:alpha="0.2"> </RelativeLayout> <TextView
  • 6. android:id="@+id/notification_colored_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#F4F420" android:textSize="14dp" android:layout_alignParentBottom="true" android:padding="6dp" android:layout_marginLeft="5dp" android:background="@drawable/button_border" android:textStyle="bold"/> </RelativeLayout> </LinearLayout> 2. We would like to link our new XML with our code, and of course update the view values (such as text and image) with those we received in the push notification JSON: // register for message events PushManager.getInstance(getApplicationContext()).registerForMessagesEvents(new PushAppsMessageInterface() { // This method will get called every time the device will receive a push message @Override public void onMessage(Context ctx, Intent intent) { // Get the title string from the push notification message String titleTxt = intent.getStringExtra(PushManager.NOTIFICATION_TITLE_KEY); // Get the message string from the push notification message String subTitleTxt = intent.getStringExtra(PushManager.NOTIFICATION_MESSAGE_KEY); String extraData = intent.getStringExtra("info"); // Your Custom JSON key String actionButton = "Click Me!"; // Some default string String imageUrl = ""; try { JSONObject jsonObject = new JSONObject(extraData); // Extract the text for our action button, from the custom JSON actionButton = jsonObject.getString("button_text"); imageUrl = jsonObject.getString("picture_url");
  • 7. } catch (JSONException e) {} // The intent to start, when the user clicks the notification Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext()); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT); // The custom view RemoteViews expandedView = new RemoteViews( getApplicationContext().getPackageName(),R.layout.custom_notification); expandedView.setTextViewText(R.id.notification_title, titleTxt); expandedView.setTextViewText(R.id.notification_subtitle, subTitleTxt); expandedView.setTextViewText(R.id.notification_colored_text, actionButton); expandedView.setImageViewBitmap( R.id.notification_main_image_view, drawableFromUrl(imageUrl)); // Building the notification that will show up in the notification center Notification notification = new NotificationCompat.Builder( getApplicationContext()) .setSmallIcon(R.drawable.small_pic) .setSound(Settings.System.DEFAULT_NOTIFICATION_URI) .setDefaults(Notification.DEFAULT_VIBRATE) .setAutoCancel(true) .setContentIntent(resultPendingIntent) .setContentTitle(titleTxt) .setContentText(subTitleTxt) .build(); notification.bigContentView = expandedView; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(1, notification); } });
  • 8. Sending a Push Notification from the PushApps Admin Console Sending a push notification from the PushApps Admin Console is very simple. You can learn a lot about it from our WIKI - http://wiki.pushapps.mobi/display/PUSHAPPS/Admin+Console. Please notice that the parameters keys that you provide in the Admin Console should match those in your Android code. Finally!
  • 9. Some minor side notes: * Notice that this code is compatible for Android devices running API 11 version or higher. There are similar solutions for custom push notifications view for older devices - just google it. * If you Notification Center in your device is full with other push notifications from other apps, the push notification view will appear in its small and regular view (without the custom view). That’s why it’s important to provide the parameters for the regular state (e.g. Small Icon, Content Title, Content Text).
  • 10. Checkout the entire source code from here. Happy Coding!