Got a little problem that's been bugging me..
I've set up my application to receive PUSH notifications from Urban Airship and that all works fine, but when I tap on a notification in the notifications center, nothing happens.
I want my app to open when a user taps a PUSH notification - what can I do to achieve this?
Any help is as always greatly appreciated.
Thanks
Create a pending Intent to start the activity and set it in notification using setLatestEventInfo.
Example:
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
More info can be found here
You need to use a custom notification builder and use one of your activities as the PendingIntent.
https://docs.urbanairship.com/android-lib/reference/com/urbanairship/push/CustomPushNotificationBuilder.html
Following one of their sample projects (https://github.com/urbanairship/android-samples/tree/master/app/src/main/java/com/urbanairship/sample), you can extend the AirshipReceiver class and then override the onReceive method. This did the trick for me:
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
String action = intent.getAction();
if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {
Intent launch = new Intent(Intent.ACTION_MAIN);
launch.setClass(UAirship.shared().getApplicationContext(), MyHome.class);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launch.putExtra("doWhatever",true);
UAirship.shared().getApplicationContext().startActivity(launch);
}
}
you have to call pendingIntent before call .setAutoCancel(true);
look at my builder :
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),channel_id).setSmallIcon(R.drawable.logo).setSound(uri).setVibrate(new long[]{1000,1000,1000,1000,1000})
.setOnlyAlertOnce(true).setContentIntent(pendingIntent).setAutoCancel(true);
Related
I want to make it so when one plugs in there headset a notification icon appears. I've made it so when the phone turns on this runs which starts the MainActivity class which has the code for the notification icon in the OnCreate method so it just automatically starts. The problem with that is that it starts the whole activity and app, which I don't want. I just want the icon to appear. How could I go about this? Thank You!
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
The above code starts the MainActivity on boot.
Notification Icon Code
//Notification Icon Starts
NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification=new Notification(R.drawable.icon_notification, "Icon Notification", System.currentTimeMillis());
Context context=MainActivity.this;
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), Notification.FLAG_ONGOING_EVENT);
notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.setLatestEventInfo(this, "Notification Icon", "Touch for more options", contentIntent);
Intent intent=new Intent(context,MainActivity.class);
PendingIntent pending=PendingIntent.getActivity(context, 0, intent, 0);
nm.notify(0, notification);
//Notification Icon Ends
As I wrote in your last question, add a <receiver> in your manifest that listens for the ACTION_HEADSET_PLUG broadcast. The documentation shows Intent extras that you can use to find out if the headset is plugged in (state), etc.
I posted a solution that allows you to monitor the headphone state (wired and bluetooth), over here. This solution could easily be extended to allow you to display a notification.
Unfortunately, I was unable to find a resolution to this issue in other StackOverflow posts so I apologize in advance for re-posting this question. Essentially, I have an AppWidget that creates a Notification. I want the user to click the Notification to launch an Activity. The AppWidget and Activity are in the same APK.
The following code is all in the AppWidget. Clicking on the Notification broadcasts a custom Intent which is received by the AppWidget.
The problem is that the Activity does not appear to launch, even though logcat demonstrates that the custom Intent has indeed been broadcast. The only clue is the logcat message reiterated in the post title.
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.BROWSER_INTENT)) {
launchActivity(context, R.layout.appwidget, R.id.launch_button, Activity.class);
}
super.onReceive(context, intent);
}
private void displayNotification(Context context, String ticker, String title, String msg) {
Intent intent = new Intent(context, ThisWidget.class);
intent.setAction(Constants.BROWSER_INTENT);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, ticker, System.currentTimeMillis());
notification.setLatestEventInfo(context, title, msg, pending);
notificationManager.notify(1, notification);
}
Some notes:
Pressing a button to execute launchActivity() works (it launches the
Activity)
launchActivity uses Intent.FLAG_ACTIVITY_NEW_TASK as
recommended by Android lifecycle documentation
Has anyone solved this issue when launching an Activity from a Notification press?
er, figures, I answered my own question just minutes after posting it. I guess I was over-complicating the implementation by using an Intent action. I changed these lines:
Intent intent = new Intent(context, ThisWidget.class);
intent.setAction(Constants.BROWSER_INTENT);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
to this:
Intent intent = new Intent(context, Activity.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
and it worked.
Sorry for the bogus post.
I would like to create a notification that clears automatically when the user presses it and has no other behaviuour. The notification below works fine but when i press on it it takes me to the activity "MyActivity" (even having to define an intent seems a bit unecessary when I don't want to use it...)
Using FLAG_AUTO_CANCEL doesn't seem to have any effect at all.
Update: Sorry, I have found the FLAG_AUTO CANCEL does work, that is the notification is cleared from the status bar. I guess I am really tring to write an intent that does nothing (or completely delete the intent).
Code...
Notification notification = new Notification(R.drawable.success, res.getString(R.string.messages_sent), System.currentTimeMillis());
//Define the expanded message and intent
Context context = getApplicationContext();
CharSequence contentTitle = res.getString("My content title");
Intent notificationIntent = new Intent(this, MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0 );
notification.setLatestEventInfo(context, contentTitle, mStrSuccessMessage, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
//Pass the notification to the notification manager
mNotificationManager.notify(1, notification);
Use an empty intent without an actual action.
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(), 0);
I don't think you can. You can have MyActivity exit immediately though: call finish() from onCreate().
I was trying to switch from tabview (those took about 1/3rd of a small screen) to fullscreens selectable via notification messages.
So far so good, everything done following the instructions on many howto's its all working like a charm. (that is on the sdk emulator)
now i've transfered the app to my actual android telephone and now it doesn't switch the screens anymore via the notifications. it ALWAYS opens the MainActivity.
private void DroiDCNotification(int NotificationID, CharSequence tickerText, CharSequence contentTitle, CharSequence contentText) {
//throw new UnsupportedOperationException("Not yet implemented");
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.droidc_icon; // icon from resources
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NotificationID, notification);
}
So how can i make the notification called by a specific Activity open that specified Activity?
I was trying to switch from tabview (those took about 1/3rd of a small screen) to fullscreens selectable via notification messages.
I have no idea why you think that this is a good idea. Notifications are not designed for this role. Please use an options menu.
So how can i make the notification called by a specific Activity open that specified Activity?
It is going to execute the PendingIntent. Your PendingIntent wraps an Intent identifying MainActivity.class. If you do want it to use MainActivity.class, change MyActivity.class to whatever class you wish.
Hi Just use this twolines in yr activity
Intent notificationIntent = new Intent(context, SamplePushActivity.class);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
I have been having a problem with a notification not opening/going to the correct activity when it has been clicked.
My notification code (located in a class which extends Service):
Context context = getApplicationContext();
CharSequence contentTitle = "Notification";
CharSequence contentText = "New Notification";
final Notification notifyDetails =
new Notification(R.drawable.icon, "Consider yourself notified", System.currentTimeMillis());
Intent notifyIntent = new Intent(context, MainActivity.class);
PendingIntent intent =
PendingIntent.getActivity(context, 0,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
((NotificationManager)getSystemService(NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, notifyDetails);
If I click the notification while the application which created the service is open, the notification disappears (due to the FLAG_AUTO_CANCEL) but the activity does not switch.
If I click the notification from the home screen, the notification disappears and my app is brought to the front, however it remains on the activity which was open before going to the home screen, instead of going to the main screen.
What am I doing wrong? How do I specify the activity that will be pulled up?
May have actually answered my own question:
Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
notifyIntent.setClass(getApplicationContext(), Main.class);