I'm using firebase cloud function for showing notification in my app and that working perfectly only issue I'm facing is when app is closed or in background notification doesn't show pop up.
Heads up notifications are used in app and I do get the notification but notification doesn't pop up as it do while app is in foreground.I have set the Priority to high for both channel and notification but still it doesn't work.
My Service Class :
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
Intent intent = new Intent(this, WebViewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "Default";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.app_logo)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true).setContentIntent(pendingIntent)
.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)
.setPriority(Notification.PRIORITY_MAX);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_HIGH);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
}
Manifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name="com.noderon.riscalert.WebViewActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.noderon.riscalert.MainActivity">
</activity>
<service
android:name=".NotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
Any help would be appreciated. Thanku
As you are already setting the Priorities it seems like you are not adding the metadata for the notification and when the app is in the background it won't get any data that's why it only showing the pop up in the foreground
add these metadata in your code:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/ic_stat_ic_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/colorAccent" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="#string/default_notification_channel_id" />
Also, you can see Google Cloud Messaging documentation for more details
Happy coding
I have faced the same issue. I have followed the official documentation from the firebase and now notifications are working fine. Follow this link
.
Have you created FirebaseMessage Instance() in your Activity?
Related
I am new to learning flutter. My flutter app has a native android background service on kotlin where I have a socket running and whenever the socket listens to something I generate a notification using NotificationCompat.Builder.
But I am unable to bring the Flutter app back to foreground when ever the user taps on the notification.
I have tried creating a pendingintent and passing it to notificationbuilder setContentIntent() but nothing happens on tap.
shownotification function on Native background service:
fun shownotification (){
packageNamer = this.getPackageName()
launchIntent = this.getPackageManager().getLaunchIntentForPackage(packageName)
var className = launchIntent?.getComponent()!!.className.javaClass
intent.action = "SELECT_NOTIFICATION"
intent.setClass(this,className)
pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)
var builder = NotificationCompat.Builder(this, "notifications")
.setOngoing(true)
.setContentIntent(pendingIntent)
.setFullScreenIntent(pendingIntent, true)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentTitle("My notification")
.setContentText("Much longer text that cannot fit one line...")
.setStyle(NotificationCompat.BigTextStyle()
.bigText("Much longer text that cannot fit one line..."))
.setPriority(NotificationCompat.PRIORITY_HIGH)
manager?.notify(456,builder.build())
}
AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name=".MyApplication"
android:label="bizzyb_demo"
android:icon="#mipmap/ic_launcher">
<service android:name=".MyService" android:exported="true" android:enabled="true"/>
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="#style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in #style/LaunchTheme). -->
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
What am I doing wrong here. I believe I am missing something very obvious here as I am new to mobile apps development.
Your expert feedbacks will be appreciated.
Thanks
You need to include click_action: FLUTTER_NOTIFICATION_CLICK as a "Custom data" key-value-pair in the notification payload.
Bundle bundle = new Bundle();
bundle.putString(Constants.EXAM_ID,String.valueOf(lectureDownloadStatus.getExamId()));
var builder = NotificationCompat.Builder(this, "notifications")
.setOngoing(true)
.setContentIntent(pendingIntent)
.setFullScreenIntent(pendingIntent, true)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentTitle("My notification")
.setContentText("Much longer text that cannot fit one line...")
.setStyle(NotificationCompat.BigTextStyle()
.bigText("Much longer text that cannot fit one line..."))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setExtras(bundle) // Set the bundle
FCM notifications are not working in Android latest version OREO.
Below is my code:
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/luncher_logo"
android:label="#string/app_name"
android:roundIcon="#mipmap/luncher_logo"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".WelcomeActivity">
<intent-filter>
<action android:name="com.example.action.MAIN" />
<category android:name="com.example.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<service android:name=".firebase.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".firebase.FirebaseIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
</manifest>
In Android Oreo making channel is important,When yo create notification you must pass the channel id,or your notification won't show up in Oreo devices.
more information
quick fix is:
use setChannelId method in notification class.
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notif_icon)
.setContentTitle("testTitle")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setChannelId("testChannelId") // set channel id
.setContentIntent(pendingIntent);
complete answer in this link.
From Android 8.0 (API level 26+), notification channels are supported and recommended.
FCM provides a default notification channel with basic settings.
If you prefer to create and use your own default channel, set default_notification_channel_id to the ID of your notification channel object as shown below.
FCM will use this value whenever incoming messages do not explicitly set a notification channel.
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="#string/default_notification_channel_id"/>
You can create channel id from this link
You need to create channels in order to display a notification in Oreo.
If you use default firebase notifications, you can add channel_id in manifest.
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="#string/default_notification_channel_id"/>
If you create notifications manually, then you need to create channel programmatically. More details on official documentation.
I have tried some solution from the stackoverflow . But not working properly in my case.
Below is my MyFirebaseMessagingService class which extends FirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
String url , title , body ;
Map<String,String> data = new HashMap<>();
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
title = remoteMessage.getNotification().getTitle();
body = remoteMessage.getNotification().getBody();
if(remoteMessage.getData().size()>0){
Log.d("DATA", remoteMessage.getData().toString());
try{
data = remoteMessage.getData();
url = data.get("url");
}
catch (Exception e){
Log.e("Error", e.toString());
}
showNotification(url , title , body);
}
}
private void showNotification(String url, String title, String body) {
Intent intent = new Intent(this, NotificationViewer.class);
intent.putExtra("URL",url);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) ;
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(body);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.drawable.logo);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
}
}
Below is my AndroidManifest.xml code .
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dcastalia.com.munshijobsportal">
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:name=".Controller.AppController"
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".activity.SplashActivity"
android:noHistory="true"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".firebase.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".firebase.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity android:name=".activity.NotificationViewer"
android:exported="true">
</activity>
</application>
</manifest>
I have tried out some solution but not working in my case. Everything is working fine when app is foreground but not working in background. I want to open a particular activity name NotificationViewer.Java activity when user click on the notification. But when my app is in background it keep opening my Launcher Activity.
You need to set click action in your json payload in notification request
'click_action' => 'YourTAG' and apply same tag in your manifest activity
<activity
android:name=".activity.NotificationViewer">
<intent-filter>
<action android:name="YourTAG" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
You are most likely using the Firebase Notification message instead of the Data message.
If this is the case, using Data FCM message should do the trick for you.
As can be seen in https://firebase.google.com/docs/cloud-messaging/concept-options
Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages on your client app.
Notification Messages:
When your application is in the foreground, it receives the payload, but when in the background, Notification messages are delivered to the notification tray.
Data Messages:
These are are always delivered to the application, regardless if it is in the foreground or in the background.
NOTE: The FCM data message alone will not bring your application in the foreground. You will need additional code that will do that upon reception of the data message.
The notification-click starts specified activity only when the app is opened up and the notification-click is performed. If the app is in background/not running and the notification-click is performed, the application's MainActivity opens up. In short, it is like the app opens normally following the activity stack instead of opening the specified activity in the PendingIntent.
I want to redirect the notification clicks to two different Activities (ApprovalDetailActivity and ConversationDetailActivity), based on their type.
I am using FCM for Push notifications. I am pasting my Manifest file and my FCMListener file here. Please help me out.
sendNotification() function in MyFirebaseMessagingService.java
private void sendNotification(String messageBody)
{
Intent intent;
System.out.println("----message body: " + messageBody);
if(notificationBundle.getCategory().equalsIgnoreCase(Master.KEY_PUSH_NOTIFICATION_CONVERSATION))
{
intent = new Intent(this, ConversationDetailActivity.class);
/*Conversation conversation = Master.notificationBundle.getConversation();
Master.conversationsList = new ArrayList<>();
Master.conversationsList.add(conversation);*/
}
else
{
intent = new Intent(this, ApprovalDetailActivity.class);
if(notificationBundle.getApprovalType().equals("I"))
intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_VERIFICATIONS);
else if(notificationBundle.getApprovalType().equals("A"))
intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_APPROVALS);
else
intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_COMPLETED);
intent.putExtra(Master.KEY_IS_FROM_CONVERSATION, false);
}
intent.putExtra(Master.KEY_PUSH_NOTIFICATION_POST_ID , notificationBundle.getPostID());
intent.putExtra(Master.KEY_IS_FROM_PUSH_NOTIFICATION, true);
intent.putExtra(Master.KEY_POSITION, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.mnet_icon)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int random = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
notificationManager.notify(random, notificationBuilder.build());
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="mnet.mediaware.com.m_net">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name=".MnetApplication"
android:allowBackup="true"
android:icon="#drawable/mnet_icon"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".activities.LoginActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:launchMode="singleTask"
android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_main"
android:launchMode="singleTask"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".activities.ConversationDetailActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_conversation_detail"
android:parentActivityName=".activities.MainActivity"
android:theme="#style/AppTheme.NoActionBar"
android:launchMode="singleTask"
android:windowSoftInputMode="stateHidden|adjustResize">
<intent-filter>
<action android:name="mnet.mediaware.com.m_net.activities.ConversationDetailActivity" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
</activity>
<activity
android:name=".activities.ApprovalDetailActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_approval_detail"
android:parentActivityName=".activities.MainActivity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustResize">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
</activity>
<activity
android:name=".activities.NewConversationActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_new_conversation"
android:parentActivityName=".activities.MainActivity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustResize">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
</activity>
<activity
android:name=".activities.NotificationActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_notification"
android:parentActivityName=".activities.MainActivity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustResize">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
</activity>
<activity
android:name=".activities.ProfileActivity"
android:label="#string/title_activity_profile"
android:configChanges="orientation|keyboardHidden|screenSize"
android:parentActivityName=".activities.MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
</activity>
<service
android:name=".utils.firebase.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".utils.firebase.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
As per Firebase Cloud Messaging documentation-If Activity is in foreground then onMessageReceived will get called. If Activity is in background or closed then notification message is shown in the notification center for app launcher activity.
For More information Check this link
Only this thing worked for me. The thing work for me is simple. Make sure you add this in the activity that you want to open directly.
<intent-filter>
<action android:name="MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
And from the push notification you must add a new payload: click_action
it will looks like this -
"notification": {
"title": "hello",
"body": "test message",
"click_action": "MAIN_ACTIVITY"
},
Note: You can name it as you want MAIN_ACTIVITY but must be same in both place.
Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
Messages with both notification and data payload, when received in the background. In this case, the notification is delivered to the device’s system tray, and the
data payload is delivered in the extras of the intent of your launcher Activity.
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()){
String value = getIntent().getExtras().getString(key);
Log.d(TAG, "Key: " + key + " Value: " + value);
}}
use this code to get intent datas
I think I got the exact answer for you.
TaskStackBuilder Usage and Definition
Please don't overlook the awesome video into the above link.
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(getContext());
taskStackBuilder.addNextIntentWithParentStack(intent);
PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
when app is in background Firebase push notification data payload deliver to Launcher Activity,
So, in Launcher Activity you can get simply check and open appropriate screen
if (intent.hasExtra("data")) {
}
Get bundle(for push notification data payload) from intent at launcher activity then start your specific activity.
I'm working with push notifications. Receiving and displaying notifications is currently working, however the on-click event isn't acting as it should.
int NOTIFICATION_ID = 1593;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification, title, System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, MyMainActivity.class);
notificationIntent.putExtra("test", "test");
notificationIntent.setAction("load_notifications");
PendingIntent intentNotification = PendingIntent.getActivity(this, 999, notificationIntent, 0);
notification.setLatestEventInfo(this, title, body, intentNotification);
notificationManager.notify(NOTIFICATION_ID, notification);
That's how I display my notification and it also includes starting/resuming my main activity.
However, the putExtra("test", "test") and the setAction seem to have no effect if I click on a notification while I'm in the app. If my app isn't working, launching it from the notification works fine.
My onResume function at my main activity:
public void onResume() {
super.onResume();
if(this.getIntent().getAction().equalsIgnoreCase("load_notifications")) {
//Do stuff
}
}
My app config is as following:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".Activity"
android:label="#string/app_name"
android:configChanges="orientation"
android:launchMode="singleInstance"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Push notifications -->
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.app.android" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
the answer maybe be here : Action buttons are not shown in notification
Notification Actions were introduced in API 16.
This means that you should be setting your target SDK to 16 or above.
However, only Jelly Bean and above can take advantage of the Notification Actions - make sure to test this feature on those platforms.
NotificationCompat.Builder takes care of generating a Notification compatible with whatever API it is running on, so keep using it if you are going to be supporting Ice Cream Sandwich and older. If not, simply using Notification.Builder will suffice (after changing the target SDK of course).