I can't enter to the app through the notification bar - android

I have an app which is counting down the time. I would like to get a notification in the notification bar while the time is up.
I've already done this:
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this).setTicker("Ticker Title").setContentTitle("Content Title").setContentText("Notification content.").setSmallIcon(R.drawable.iconnotif).setContentIntent(pIntent).getNotification();
noti.flags=Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(uniqueID, noti);
The problem occurs when I try to enter the app by clicking the notification.
When the notification is shown, I click it, but it doesn't do anything.
How to solve this problem?
Thanks for help! :)

Your Notification has a PendingIntent. This is used to define behaviour for clicking the notification. A pending intent has an intent as well, this intent contains info about application to launch, or in general, what to do after clicking the notification.
In your example, however, the intent that is included in your Pending Intent is:
// Empty intent, not doing anything
Intent intent = new Intent();
E.g. your intent does not define what to do. Change your intent to something like this:
// New Intent with ACTION_VIEW:
Intent intent = new Intent(Intent.ACTION_VIEW);
// Activity to launch
intent.setClassName("your.package.name", "ActivityToLaunch");
// Intent Flags
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Related

Keep my main Activity when starting from Notification Intent

In my App, I have MainActivity with bottom navigation. When I receive a new notification, when I click on it (to open it), I don't want to recreate my MainActivity if it already exists. So I tried to add flags, but it doesn't work at all...
Could you help me guys?
Intent newsFeedDetailsActivityIntent = NewsFeedDetailsActivity.newInstance(mNewsFeedId);
Intent mainActivityIntent = MainActivity.newInstance(MainActivity.createBundle(navItem, currentMs));
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder builder = TaskStackBuilder.create(MyApplication.getInstance())
.addNextIntentWithParentStack(mainActivityIntent)
.addNextIntent(newsFeedDetailsActivityIntent);
PendingIntent pendingIntent = builder.getPendingIntent((int) System.currentTimeMillis(), PendingIntent.FLAG_UPDATE_CURRENT);
// Set pending intent to notification
notification.setContentIntent(pendingIntent);

interrupting activity and multiple instances of activity issues

My problems are hopefully related to each other. I run a countdown timer service that fires an intent in the onfinish method. When on finish completes it automatically opens/ bring the app up which I don't want it to do. why is this so? id like it to just show the notification. Also when I click on the notification instead of just resuming the activity it creates another one. I figured the problems were related to each other due to the intents. here is the code
from onFinish() not from the notification
Intent intent = new Intent(BroadcastService.this, MainActivity.class);
intent.putExtra("id1",id1);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
and here is the code for the notification
NotificationCompat.Builder notificBuilder = new NotificationCompat.Builder(this);
notificBuilder.setContentTitle("Loot");
notificBuilder.setContentText("Claim your Gold now!");
notificBuilder.setSmallIcon(R.drawable.gold2);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificBuilder.setSound(soundUri);
notificBuilder.setAutoCancel(true);
Intent backhome = new Intent(this, MainActivity.class);
backhome.putExtra("id2",id2);
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
taskStackBuilder.addParentStack(MainActivity.class);
taskStackBuilder.addNextIntent(backhome);
PendingIntent pd = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
notificBuilder.setContentIntent(pd);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notifID, notificBuilder.build());
isNoteActive = true;
}
If you don't want to open your MainActivity, then you shouldn't write this code:
Intent intent = new Intent(BroadcastService.this, MainActivity.class);
...
startActivity(intent);
Opening MainActivity is exactly what this does.

How to open non-launcher activity on notification group click

Currently I have this:
Intent resultIntent = new Intent(context, DigestPager.class);
and this is opening DigestPager activity (which is not launcher activity) when clicking on a single notification.
But when there are multiple notifications and they are grouped into one and collapsed, clicking on it is opening launcher activity.
Is there some simple way to open a different activity?
Intent resultIntent = new Intent(context, DigestPager.class);
resultIntent.putExtra("digestId", digest.getDigestId());
PendingIntent pendingIntent = PendingIntent.getActivity(this, digest.getDigestId(), resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(digest.getDigestId(), mBuilder.build());
Create a handler activity that opens everytime you click on a notification and based on the flags set to the notification, you can navigate to the required activity/fragment.
Hope this helps.

Get message of notification and send to the activity

Here is i am creating notification with the message "wake up wake up !!!"
What i want to do when notification comes on particular time and user clicks on it i need to get that message "wake up wake up !!!" and send to SnoozeActivity where i am creating custom alert dialog and i want to show that message into that dialog
alarmNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, SnoozeActivity.class), 0);
NotificationCompat.Builder alamNotificationBuilder = new NotificationCompat.Builder(
this).setContentTitle("Alarm").setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle().bigText("wake up wake up !!!"))
.setContentText(msg).setAutoCancel(true);
alamNotificationBuilder.setContentIntent(contentIntent);
alarmNotificationManager.cancelAll();
alarmNotificationManager.notify(1, alamNotificationBuilder.build());
But i am not able to figure out how to get the message/data of clicked notification and send to next activity?
An Intent can carry data which you attach via Intent.putExtra()like this:
Intent intent = new Intent(this, SnoozeActivity.class);
intent.putExtra("com.example.mycoolapp.MYDATA", "wake up wake up !!!");
Then you continue with
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
In the onCreate() method of your SnoozeActivity, you can query the Intent for extras:
Intent intent = getIntent();
if (intent.hasExtra("com.example.mycoolapp.MYDATA"))
{
String myText = intent.getStringExtra("com.example.mycoolapp.MYDATA");
}
It's recommended that the keys you use for Intent extras start with your package name.
NOTE not every kind of data is possible with putExtra(), please see the documentation

how to start aplication or bring to front when user clicks on a notification click on android

I'm working whit push notifications.
When a user clicks on a notification I want to start the application in case it is not started, or bring it to front in case it is started.
Thanks
this is the complite code I found the answer here Bring application to front after user clicks on home button
Intent intent = new Intent(ctx, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT),
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
ctx).setContentTitle(extras.getString("title"))
.setContentText(extras.getString("message"))
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent);
Notification noti = mBuilder.build();
noti.flags = Notification.DEFAULT_LIGHTS
| Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(NOTIFICATION_ID, noti);
The important things are the flags on the Intent, this will open the app or bring it to front if is opened, or do nothing if you click on the notification while you are browsing the app
implement pending intent.
Code
Intent pi = new Intent();
pi.setClass(getApplicationContext(), youactivity.class);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,pi, PendingIntent.FLAG_UPDATE_CURRENT);
String msgText = mMessage;
// construct the Notification object.
Notification notif = new Notification(R.drawable.icon, msgText,System.currentTimeMillis());
manifest
<activity android:name="com.InfoActivity" android:noHistory="false android:excludeFromRecents="false"></activity>
Just set the intent for the notification, this is covered in details in the official API guide.
You do that by creating a PendingIntent.
For example, this will launch a MainActivity when notification is clicked.
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this)
.setContentTitle("Notication title")
.setContentText("Content text")
.setContentIntent(pendingIntent).build();
I don't know whether you're talking about Google Cloud Messaging or not. But if it is a normal Notification then while creating your notification you've to provide Pending Intent. Just put your desired class in Pending intent so that when user clicks on that notification you'll be driven to your so called Application. A snippet :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,
"Your application activity".class), PendingIntent."Flag you want");
After this use this intent while creating notification. It's method is setContentIntent("above intent") and fire your notification with NotificationManager!

Categories

Resources