firebase notification get only inapp subscribers - android

i am new for android development, i am design one app, in that app in-app subscription option available, after subscription user need to get notification, but all installed users get the notification, i need only subscriber users get the notification, how to resolve this issue any one help me this,
public class FirebaseMessageReceiver extends FirebaseMessagingService {
// Override onMessageReceived() method to extract the
// title and
// body from the message passed in FCM
Prefs prefss;
#Override
public void
onMessageReceived(RemoteMessage remoteMessage) {
// First case when notifications are received via
// data event
// Here, 'title' and 'message' are the assumed names
// of JSON
// attributes. Since here we do not have any data
// payload, This section is commented out. It is
// here only for reference purposes.
/*if(remoteMessage.getData().size()>0){
showNotification(remoteMessage.getData().get("title"),
remoteMessage.getData().get("message"));
}*/
// Second case when notification payload is
// received.
prefss = new Prefs(this);
if (remoteMessage.getNotification() != null) {
// Since the notification is received directly from
// FCM, the title and the body can be fetched
// directly as below.
if (prefss.getPremium() == 1) {
showNotification(
remoteMessage.getNotification().getTitle(),
remoteMessage.getNotification().getBody());
}
}
}
// Method to get the custom Design for the display of
// notification.
private RemoteViews getCustomDesign(String title,
String message) {
RemoteViews remoteViews = new RemoteViews(
getApplicationContext().getPackageName(),
R.layout.notification);
remoteViews.setTextViewText(R.id.title, title);
remoteViews.setTextViewText(R.id.message, message);
remoteViews.setImageViewResource(R.id.icon,
R.drawable.logo);
return remoteViews;
}
// Method to display the notifications
public void showNotification(String title,
String message) {
// Pass the intent to switch to the MainActivity
Intent intent
= new Intent(this, Subscription.class);
// Assign channel ID
String channel_id = "notification_channel";
// Here FLAG_ACTIVITY_CLEAR_TOP flag is set to clear
// the activities present in the activity stack,
// on the top of the Activity that is to be launched
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Pass the intent to PendingIntent to start the
// next Activity
PendingIntent pendingIntent
= PendingIntent.getActivity(
this, 100, intent,
PendingIntent.FLAG_ONE_SHOT);
// Create a Builder object using NotificationCompat
// class. This will allow control over all the flags
NotificationCompat.Builder builder
= new NotificationCompat
.Builder(getApplicationContext(),
channel_id)
.setSmallIcon(R.drawable.logo)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setVibrate(new long[]{1000, 1000, 1000,
1000, 1000})
.setOnlyAlertOnce(true)
.setPriority(NotificationCompat.PRIORITY_HIGH);
// A customized design for the notification can be
// set only for Android versions 4.1 and above. Thus
// condition for the same is checked here.
if (Build.VERSION.SDK_INT
>= Build.VERSION_CODES.JELLY_BEAN) {
builder = builder.setContent(
getCustomDesign(title, message));
} // If Android Version is lower than Jelly Beans,
// customized layout cannot be used and thus the
// layout is set as follows
else {
builder = builder.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.logo);
}
// Create an object of NotificationManager class to
// notify the
// user of events that happen in the background.
NotificationManager notificationManager
= (NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);
// Check if the Android Version is greater than Oreo
if (Build.VERSION.SDK_INT
>= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel
= new NotificationChannel(
channel_id, "web_app",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(
notificationChannel);
}
notificationManager.notify(1, builder.build());
}
}

Related

Construct Notification for Xamarin Android Application

Am trying to create a notification that will notify user when alarm matures, am calling the Notification code in my OnCreate method, so i expect to see it when i launch my Activity but my code here seems to have a problem, any help to get the App to notify will greatly be appreciated...
Here is what i got so far
class SecondActivity : AppCompatActivity
{
static readonly int mid = 1000;
static readonly string CHANNEL_ID = "location_notification";
protected override void OnCreate(Bundle onSavedInstanceState){
//Notification code
NotificationChannel channel = null;
Intent intent=new Intent(this, typeof(SecondActivity));
//Construct TaskStack builder for pending intent
Android.App.TaskStackBuilder taskStackBuilder = Android.App.TaskStackBuilder.Create(this);
//Add intent to backstack
taskStackBuilder.AddNextIntentWithParentStack(intent);
//Construct pending intent to open desired activity
PendingIntent pendingIntent = taskStackBuilder.GetPendingIntent(0, PendingIntentFlags.UpdateCurrent);
//Enque notification to inform the user that the alarm has matured
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.SetSmallIcon(Resource.Drawable.abc_tab_indicator_mtrl_alpha)
.SetContentTitle("Alarm")
.SetContentIntent(pendingIntent);
.SetContentText("Alarm Time has matured");
NotificationManager notificationManager =
(NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Notify(mid, mBuilder.Build());
channel = notificationManager.GetNotificationChannel(channelName);
notificationManager.CreateNotificationChannel(channel);
}
}
What Am i doing wrong?, Thanks
am calling the Notification code in my OnCreate method
You could not call notification on OnCreate method directly. Generaly We will use button click event or another separated task event to call Notification.
Second, as SushiHangover's said, you need to CreateNotificationChannel before publish
local notification.
You can refer to Notification channels to add notification channel:
void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
var channelName = Resources.GetString(Resource.String.channel_name);
var channelDescription = GetString(Resource.String.channel_description);
var channel = new NotificationChannel(CHANNEL_ID, channelName, NotificationImportance.Default)
{
Description = channelDescription
};
var notificationManager = (NotificationManager) GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
And pulish notification:
// Instantiate the builder and set notification elements:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.SetContentTitle ("Sample Notification")
.SetContentText ("Hello World! This is my first notification!")
.SetSmallIcon (Resource.Drawable.ic_notification);
// Build the notification:
Notification notification = builder.Build();
// Get the notification manager:
NotificationManager notificationManager =
GetSystemService (Context.NotificationService) as NotificationManager;
// Publish the notification:
const int notificationId = 0;
notificationManager.Notify (notificationId, notification);
Note: The CHANNEL_ID should be the same both on creating channel and publishing notification. Generally, we will use the package name as the channel id.
The full sample code as follows:
private void Button_Click(object sender, EventArgs e)
{
// create notification channel
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
var channelName = "Notify user";
var channelDescription = "first local notification";
var channel = new NotificationChannel("com.companyname.appandroidlistview", channelName, NotificationImportance.Default)
{
Description = channelDescription
};
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "com.companyname.appandroidlistview")
.SetContentTitle("Sample Notification")
.SetContentText("Hello World! This is my first notification!")
.SetSmallIcon(Resource.Drawable.icon);
// Build the notification:
Notification notification = builder.Build();
// Get the notification manager:
NotificationManager notificationManager =
GetSystemService(Context.NotificationService) as NotificationManager;
// Publish the notification:
const int notificationId = 0;
notificationManager.Notify(notificationId, notification);
}
You need to do a lot of things before your notification can work, Microsoft documentation provides a very easy way of doing that...
class SecondActivity : AppCompatActivity
{
//Declare notification ID and Channel ID In your class so you can use them from any method
static readonly int NOTIFICATION_ID = 1000;
static readonly string CHANNEL_ID = "location_notification";
protected override void OnCreate(Bundle savedInstanceState){
}
//Define the method you will use to call notification
}
void createNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
var name = Resources.GetString(Resource.String.channel_name);
var description = GetString(Resource.String.channel_description);
var channel = new NotificationChannel(CHANNEL_ID, name, NotificationImportance.Default)
{
Description = description
};
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
//Use a button to send the notification to the operating system
private void notifier(object sender, EventArgs e){
Intent intent=new Intent(this, typeof(SecondActivity));
//Construct TaskStack builder for pending intent
Android.App.TaskStackBuilder taskStackBuilder = Android.App.TaskStackBuilder.Create(this);
//Add intent to backstack
taskStackBuilder.AddNextIntentWithParentStack(intent);
//Construct pending intent to open desired activity
PendingIntent pendingIntent = taskStackBuilder.GetPendingIntent(0, PendingIntentFlags.UpdateCurrent);
//Enque notification to inform the user that the alarm has matured
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.SetSmallIcon(Resource.Drawable.abc_tab_indicator_mtrl_alpha)
.SetContentTitle("Alarm")
.SetContentText("Alarm Time has matured")
.SetShowWhen(false).SetContentIntent(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(NOTIFICATION_ID, mBuilder.Build());
}
}
}
If you follow the instructions on this page then your notification should show without so much hassle
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/notifications/local-notifications-walkthrough

Notification in Samsung and Xiaomi show This app hasn't send you any notification

I have a problem with notifications where the notification doesn't appear on my xiaomi and samsung devices but works for the other device. I've tried what people recommend like trying auto start, managing battery settings, but it still doesn't appear on both devices. I also tried to use the notify library but the results were the same. Strangely, when I checked the notification setting and then clicked on the application, it appeared as shown below
samsung
This is my notification code :
public void showNotification() {
String appname = "App Name";
String title = "Notification Title";
String text = "This is the notification text";
String iconUrl = "http://url.to.image.com/image.png";
NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
TaskStackBuilder stackBuilder = TaskStackBuilder.from(MainActivity.this);
stackBuilder.addParentStack(NewsActivity.class);
stackBuilder.addNextIntent(new Intent(MainActivity.this, NewsActivity.class));
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
builder.setContentTitle(title).setContentInfo(appname).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo_wanda_flp)).setContentText(text).setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.logo_wanda_flp);
notifyManager.notify("textid", 123, builder.getNotification());
}
The code won't work on Android 8 (API 26) and above without first registering a notification channel. On Android before that, it will work just fine.
Some of the code snippets below are taken from here.
Creating a notification channel
Register notification channels inside your activity before attempting to show any notification. Alternatively, you can register them inside a custom Application class before any activities or services start (but this won't be shown here for the sake of simplicity).
#Override
public void onCreate(Bundle savedInstanceState) {
createNotificationChannel(); // Registering a notification channel
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
Deliver the notification
public void showNotification() {
// ...
// NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
// Replace with line below.
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID);
// ...
}

FCM Tap on how to open a User specified activity instead of default activity when app is in background state

The FCM is working fine and notification came on device when app is in foreground state, and when tapped on notification, it is redirecting to my specified Activity, so it is working fine.
But my challenge is when the notification comes when app is in background state and when tapped, it redirects to Default Activity but I want to navigate to specified activity.
Here is MyFirebaseMessagingService class:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
private String title, messageBody;
/**
* Called when message is received.
*
* #param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// [START_EXCLUDE]
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if (remoteMessage.getData() != null && remoteMessage.getData().size() > 0) {
title = remoteMessage.getData().get("title");
if (TextUtils.isEmpty(title)) title = "Bocawest";
messageBody = remoteMessage.getData().get("message");
}
handleNow();
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
if (!TextUtils.isEmpty(messageBody))
sendNotification(title, messageBody);
//sendNotification(remoteMessage.getNotification().getBody());
Intent intent = new Intent();
intent.setAction("com.android.bocawest");
sendBroadcast(intent);
}
// [END receive_message]
/**
* Handle time allotted to BroadcastReceivers.
*/
private void handleNow() {
Log.d(TAG, "Short lived task is done.");
}
/**
* Create and show a simple notification containing the received FCM message.
*
* #param messageBody FCM message body received.
*/
private void sendNotification(String title, String messageBody) {
PendingIntent pendingIntent;
if (SharedPreference.getBoolean(getApplicationContext(), getApplicationContext().getResources().getString(R.string.sp_isLoginIN))) {
Intent intent = new Intent(this, NotificationsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
} else {
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Bocawest",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
}
}
Note : NotificationsActivity is my specified activity.
HomeActivity is Default Activity
I know there are lot of similar questions but I haven't found anything specific to my usecase.
Please Help me.
#Laxman parlapelly as per Firebase standered when your app receive notification in background and user tap on notification then it will open default activity only.
If you want to open your specified activity then you have to pass through your default activity only.
For example in your case when user tap on notification it will open your Home activity and from oncreate method of HomeActivity you need to open NotificationsActivity(along with bundle incase needed)
When
Notification is tapped when app is in background then onCreate() method of HomeActivity will be called so with in that you can write code to open Notification Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
animLay = findViewById(R.id.root_lay_la);
Intent intent = new Intent(this,NotificationActivity.class);
//intent.putExtra("KEY",getIntent().getStringExtra("data")); if u need to pass data
startActivity(intent);
}
if(SharedPreference.getBoolean(getApplicationContext(), getApplicationContext().getResources().getString(R.string.sp_isLoginIN))) write this logic in HomeActivity(in onCreate() before setContentView()) so every time user will be re-directed to HomeActivity and if the above condition satisfies the user will be redirected again to NotificationsActivity else will continue with HomeActivity
check - Navigate to different activities on notification click
this works for me
- just add the code below inside onMessageReceived()
Intent intent = new Intent(this, NotificationsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "111")
.setSmallIcon(R.drawable.logo)
.setContentTitle(getString(R.string.yhnn))
.setContentText(title)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setSound(sound)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(5, builder.build());

Heads up with reply (RemoteInput) does not cancel notification

Step1 : Build Notification with Reply intent and heads up notification
private void buildInlineReplyNotification() {
// Create an instance of RemoteInput.Builder that you can add to your notification action.
// This class's constructor accepts a string that the system uses as the key for the text input.
// Later, your handheld app uses that key to retrieve the text of the input.
RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY).setLabel(
getResources().getString(R.string.reply_label)).build();
// Attach the RemoteInput object to an action using addRemoteInput().
NotificationCompat.Action compatAction =
new NotificationCompat.Action.Builder(R.mipmap.ic_reply,
getResources().getString(R.string.reply), replyPendingIntent).addRemoteInput(
remoteInput).setAllowGeneratedReplies(true).build();
// Build the notification and add the action.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_notification)
.setContentTitle(
getResources().getString(R.string.notification_created) + mNotificationId)
.setContentText(getResources().getString(R.string.type_reply))
.setShowWhen(true)
.addAction(compatAction);
mBuilder.setPriority(Notification.PRIORITY_HIGH).setVibrate(new long[0]);
// Issue the notification.
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(mNotificationId, mBuilder.build());
}
Step2 : cancel after reply from notification
private void updateNotification(Context context, int notifyId) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context).setSmallIcon(R.mipmap.ic_notification)
.setContentText(context.getString(R.string.message_sent));
notificationManager.cancel(notifyId);
}
Issues is not cancel notification by notificationManager.cancel(notifyId) but if i remove this mBuilder.setPriority(Notification.PRIORITY_HIGH).setVibrate(new long[0]); Than work perfect so, what is issue with priority in notification ?

Open app after clicking on Notification

I have a notification in my app with the following code:
public class NewMessageNotification {
private static final String NOTIFICATION_TAG = "NewMessage";
public static void notify(final Context context,
final String exampleString,final String boday ,final int number) {
final Resources res = context.getResources();
// This image is used as the notification's large icon (thumbnail).
// TODO: Remove this if your notification has no relevant thumbnail.
final Bitmap picture = BitmapFactory.decodeResource(res, R.drawable.billsms);
final String ticker = exampleString;
final String title = res.getString(
R.string.new_message_notification_title_template, exampleString);
final String text = boday;
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
// Set appropriate defaults for the notification light, sound,
// and vibration.
// Set required fields, including the small icon, the
// notification title, and text.
.setSmallIcon(R.drawable.billsms)
.setContentTitle(title)
.setContentText(text)
// All fields below this line are optional.
// Use a default priority (recognized on devices running Android
// 4.1 or later)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Provide a large icon, shown with the notification in the
// notification drawer on devices running Android 3.0 or later.
.setLargeIcon(picture)
// Set ticker text (preview) information for this notification.
.setTicker(ticker)
// Show a number. This is useful when stacking notifications of
// a single type.
.setNumber(number)
// If this notification relates to a past or upcoming event, you
// should set the relevant time information using the setWhen
// method below. If this call is omitted, the notification's
// timestamp will by set to the time at which it was shown.
// TODO: Call setWhen if this notification relates to a past or
// upcoming event. The sole argument to this method should be
// the notification timestamp in milliseconds.
//.setWhen(...)
// Set the pending intent to be initiated when the user touches
// the notification.
.setContentIntent(
PendingIntent.getActivity(
context,
0,
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")),
PendingIntent.FLAG_UPDATE_CURRENT))
// Show expanded text content on devices running Android 4.1 or
// later.
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(text)
.setBigContentTitle(title)
.setSummaryText("Dummy summary text"))
// Example additional actions for this notification. These will
// only show on devices running Android 4.1 or later, so you
// should ensure that the activity in this notification's
// content intent provides access to the same actions in
// another way.
.addAction(
R.drawable.ic_action_stat_share,
res.getString(R.string.action_share),
PendingIntent.getActivity(
context,
0,
Intent.createChooser(new Intent(Intent.ACTION_SEND)
.setType("text/plain")
.putExtra(Intent.EXTRA_TEXT, "Dummy text"), "Dummy title"),
PendingIntent.FLAG_UPDATE_CURRENT))
.addAction(
R.drawable.ic_action_stat_reply,
res.getString(R.string.action_reply),
null)
// Automatically dismiss the notification when it is touched.
.setAutoCancel(true);
notify(context, builder.build());
}
#TargetApi(Build.VERSION_CODES.ECLAIR)
private static void notify(final Context context, final Notification notification) {
final NotificationManager nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
nm.notify(NOTIFICATION_TAG, 0, notification);
} else {
nm.notify(NOTIFICATION_TAG.hashCode(), notification);
}
}
/**
* Cancels any notifications of this type previously shown using
* .
*/
#TargetApi(Build.VERSION_CODES.ECLAIR)
public static void cancel(final Context context) {
final NotificationManager nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
nm.cancel(NOTIFICATION_TAG, 0);
} else {
nm.cancel(NOTIFICATION_TAG.hashCode());
}
}
}
My notifications fires very well, but my problem is that, when I click on notification in Notification Center, it does not start my app.
Basically, after clicking on my notification nothing happens!Thanks.
Basically your Pending Intent should have your application's one of the activity components.
Intent appIntent = new Intent(context, YourActivityName.class);
PendingIntent.getActivity(context, 0, appIntent, PendingIntent.FLAG_UPDATE_CURRENT));
Similarly you can add other components like Activity, Service and Receiver.
PendingIntent.getActivity ...
PendingIntent.getBroadcast ...
PendingIntent.getService ...
Hope this helps.
I found the answer to my question.
The only change this code :
.setContentIntent(
PendingIntent.getActivity(
context,
0,
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")),
PendingIntent.FLAG_UPDATE_CURRENT))
To this code :
.setContentIntent(
PendingIntent.getActivity(
context,
0,
new Intent(context.getApplicationContext(),MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT))

Categories

Resources