android studio firebasemessagingservice pendingintent not working in kill mode - android

everything works as well when the app already run, but in kill mode, nothing happened when i click on the notification, the click only remove the notification with no crashing
JSONObject oData = hashClass.HashToJson(new HashMap<>(data));
Class<?> c = Class.forName(Objects.requireNonNull(notification.getClickAction()));
Intent intent = new Intent(this, c);
intent.putExtra(NOTIFICATION, oData.toString());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

Related

App launch from widget if app is not running

I create a home screen widget whenever my app launches. But when I kill the app, and click on my widget, I need to launch the app. When the app is running in background or foreground, I must be able to do actions from the widget. My code for pending intent is -
public static PendingIntent buildButtonPendingIntent(Context context) {
++MyWidgetReceiver.clickCount;
// initiate widget update request
Intent intent = new Intent();
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
return PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
//App launch if app is not running
Intent iSetting = new Intent(context, App.class);
PendingIntent piSetting = PendingIntent.getActivity(context, 0, iSetting, 0);
}
How do I do this for app killed and app launching from widget. My code is sort of incomplete.

Skip app for foreground when clicking on notification, without opening new

What I wanted to do was open the app by notification in two cases:
Case open app : Just pull background
Case closed app: Opens new app
The code that I have is the next:
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String title = context.getString(R.string.notification_title);
String message = context.getString(getNotificationText());
Bitmap largeIcon = getLargeBitmap();
NotificationCompat.Builder notification =
(NotificationCompat.Builder) new NotificationCompat.Builder(context).setContentTitle(title).setContentText(message)
.setSmallIcon(getIcon()).setLargeIcon(largeIcon).setOngoing(true);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
Intent intent = new Intent(context, MainActivity.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(contentIntent);
}
manager.notify(NETWORK_STATUS_NOTIFICATION_ID, notification.build());
}
The problem I have is that the code is working on Android 5 and up, but does not work on Android : JELLY_BEAN and KITKAT .
Someone help me?
Add flag FLAG_ACTIVITY_NEW_TASK to your intent :
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
From the docs :
When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in.

NullPointerException after Notification is clicked in notification bar(PendingIntent)

I am trying to get my PendingIntent working but whenever I click the notification in the notification drawer, my app crashes.
So basically I have this java class called CustomerCurrentlyServing.class and when it is loaded, it retrieves all the strings passed from the previous activity as shown below:
queueNo = String.valueOf(getIntent().getExtras().getInt(Constants.EX_MSG_QUEUE_NO));
queueKey = getIntent().getExtras().getString(Constants.EX_MSG_QUEUE_KEY);
shopName = getIntent().getExtras().getString(Constants.EX_MSG_SHOP_NAME);
shopKey = getIntent().getExtras().getString(Constants.EX_MSG_SHOP_KEY);
customerid = getIntent().getExtras().getString(Constants.EX_MSG_CUSTOMER_ID);
I implemented a method for the notification as shown below:
public void showNotification() {
Intent intent = new Intent(getApplicationContext(), CustomerCurrentServing.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, CustomerCurrentServing.class), 0);intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
intent.putExtra(Constants.EX_MSG_QUEUE_KEY,queueKey);
intent.putExtra(Constants.EX_MSG_SHOP_NAME,shopName);
intent.putExtra(Constants.EX_MSG_SHOP_KEY,shopKey);
Resources r = getResources();
Notification notification = new NotificationCompat.Builder(this)
.setTicker(r.getString(R.string.notification_title))
.setSmallIcon(android.R.drawable.ic_menu_report_image)
.setContentTitle(r.getString(R.string.notification_title))
.setContentText(r.getString(R.string.notification_text))
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
It is basically opening the same class when the user clicks on the notification in the notification drawer. However, it crashes whenever I click on the notification and the error is NullPointerException on the line:
queueNo = String.valueOf(getIntent().getExtras().getInt(Constants.EX_MSG_QUEUE_NO));
Any idea how to solve this error ? I have been trying to solve this error for a week and your help is greatly appreciated.
You should putExtra on the Intent you pass to PendingIntent
Intent intent = new Intent(getApplicationContext(), CustomerCurrentServing.class);
intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
intent.putExtra(Constants.EX_MSG_QUEUE_KEY,queueKey);
intent.putExtra(Constants.EX_MSG_SHOP_NAME,shopName);
intent.putExtra(Constants.EX_MSG_SHOP_KEY,shopKey);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent , 0);
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, CustomerCurrentServing.class), 0);
here you are passing new instance of Intent
you should pass already created instance i.e.
Intent intent = new Intent(getApplicationContext(), CustomerCurrentServing.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
and write all those putExtras before creating PendingIntent object.
In this part of the code you are adding the extras to the wrong intent , you are creating an intent then creating a new one that you pass to the pending intent!
What you should be doing is this:
Intent intent = new Intent(getApplicationContext(), CustomerCurrentServing.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
intent.putExtra(Constants.EX_MSG_QUEUE_KEY,queueKey);
intent.putExtra(Constants.EX_MSG_SHOP_NAME,shopName);
intent.putExtra(Constants.EX_MSG_SHOP_KEY,shopKey);
And not creating a new intent like you did in your pending intent :
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, CustomerCurrentServing.class), 0);
intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
If you do this you are putting the extras om the first intent then passing a whole new one without extras to your pending intent so when the user clicks on the notification and it opems up the activity the activity will try and get extras that dont exist thus giving a null pointer.

Save to DB after clear the push notification android

I am working with push notification and I have one problem.
When I click in the received notification my code launch a new intent with an activity.
I use:
Intent i = new Intent(getApplicationContext(), DemoActivity.class);
i.putExtra("msg",msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
i, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
ITS OK FOR ME
But if I want only save this notification in SQLite how can I pass the message?
I know that with:
Intent bd = new Intent(getApplicationContext(), AbaseDatos.class);
bd.putExtra("msg",msg);
PendingIntent contentIntentbd = PendingIntent.getActivity(this, 0,
bd, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setDeleteIntent(contentIntentbd);
Call a new activity when clear the notification, but the new activity open the layout.
I dont want any new activity be launched. Its possible launch a javaclass with an Intent?
Any idea?
You can launch a service with an intent.
http://developer.android.com/reference/android/app/PendingIntent.html#getService(android.content.Context%2C%20int%2C%20android.content.Intent%2C%20int)

Android AppWidget + android:clearTaskOnLaunch="true"

I want my app to show a login-screen every time it is opened. With android:clearTaskOnLaunch="true" everything works fine. But there is a bug which is happening only if the phone was shutdown and the app was first started with the widget.
Intent intent = new Intent(context, StartActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
[....]
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_onebyone)
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
This code should start my mainactivity. At first start is does. But ending the app with HOME and restart it with the widget causes my app to not start with my startactivity but with another activity of my app without showing the loginscreen.
What can I do to fix it.
I've tried
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
But it does not work. All I want is to (re)-start the StartActivity-Intent like in startActivity(..) but I found no option to give my Widget an OnClickListener which starts a "normal" Activity.
Best regards,
Till
Have you tried adding the Flag to the intent:
Intent intent = new Intent(context, StartActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
[....]
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_onebyone)
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
Intent API
setFlags(int)
FLAG_ACTIVITY_CLEAR_TASK

Categories

Resources