I am using the following code to launch a notification when a Service is started Via AlarmManager:
nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); CharSequence from = "App"; CharSequence message = "Getting Latest Info..."; PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0); Notification notif = new Notification(R.drawable.icon, "Getting Latest Info...", System.currentTimeMillis()); notif.setLatestEventInfo(this, from, message, contentIntent); nm.notify(1, notif);
How do I set an intent for this item so that when the user clicks on it, it would launch a certain activity?
you have to change the intent flag as android:launchMode="singleTask" in the manifest and override onNewIntent(Intent intent) inside the activity. this will enable you to receive the parameters.
You need to grant access to your app to read notifications: "Settings > Security > Notification access" and check your app. Show activity on this post.
Build and issue the notification: Create an Intent that starts the Activity . Set the Activity to start in a new, empty task by calling setFlags() with the flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK . Create a PendingIntent by calling getActivity() .
A notification listener service allows the Google App to intercept notifications posted by other applications. Notification Options in the Google App Notification Listener Services. Within the Android Manifest file is the inclusion of the new Notification Listener Service.
As for yoshi24's comment, you may be able to set extras like this.
final Intent intent = new Intent(this, MyActivity.class); intent.setData(data); intent.putExtra("key", "value"); final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
You need to be aware of this as well before going for pending intents
https://stackoverflow.com/questions/1198558/how-to-send-parameters-from-a-notification-click-to-an-activity
UPDATE some thing like this will work for you
int your mainfest
<activity android:name=".MyActivity" android:launchMode="singleTop" ... />
in your activity
@Override protected void onCreate(Bundle savedInstanceState) { processIntent(getIntent()); } @Override protected void onNewIntent(Intent intent) { processIntent(intent); }; private void processIntent(Intent intent){ //get your extras }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With