开发者

How to launch an activity when notification is clicked?

开发者 https://www.devze.com 2023-03-06 09:03 出处:网络
I have a strange problem in my app in android. I have do开发者_JAVA技巧ne a notification and I want to launch a new activity when notification is clicked. The problems is that when I click on notifica

I have a strange problem in my app in android. I have do开发者_JAVA技巧ne a notification and I want to launch a new activity when notification is clicked. The problems is that when I click on notification nothing happens, and I have no idea where is the problem? Can anyone help me?Here is my code :

notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence NotificationTicket = "Notification";
CharSequence NotificationTitle = "Notification";
CharSequence NotificationContent = "Test";
long when = System.currentTimeMillis();

Notification notification = new Notification(R.drawable.icon,
NotificationTicket, when);

Context context = getApplicationContext();

Intent notificationIntent = new Intent(this, ShopsOnMap.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);

notification.setLatestEventInfo(context, NotificationTitle, NotificationContent, contentIntent); 
notificationManager.notify(NOTIFICATION_ID, notification);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP); 


I had the same problem and then realized that I hadn't declared my new activity in the Manifest.

<activity
        android:name=".YourActivityHere">
        </activity>


you need set action and category for Intent.

if this activity isn't the entry point of the application:

Intent notificationIntent = new Intent(context, ShopsOnMap.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent myIntent = PendingIntent.getActivity(context, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

and if is the entry point of your application, you have it by xml, like it:

<activity
        android:name=".ShopsOnMap"
        android:theme="@android:style/Theme.NoTitleBar"
        android:windowSoftInputMode="adjustResize"
          android:configChanges="orientation"
        android:screenOrientation="portrait"
         >
        <intent-filter>
            <category android:name="android.intent.category.LAUNCHER" />

            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
</activity>

so, so, only need add it:

Intent notificationIntent = new Intent(context, ShopsOnMap.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent myIntent = PendingIntent.getActivity(context, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

it works for me.


Now i know this question is VERY old, but for me a solution was to change the requestCode. Apparently it doesn't work sometimes when the requestCode is 0. It worked for me on one phone but didn't work on the other.


As in the comments: I can't see anything wrong with your code, and suspect your shopsonmap to just not show anything. Below code that I use and that works.

private void setNotifiy(){
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

    int icon = R.drawable.notification_icon;//  R.drawable.notification_icon;
    CharSequence tickerText = "Tickertext goes here :) !";
    long when = System.currentTimeMillis();
    Context context = getApplicationContext();
    CharSequence contentTitle = "ThisIsYourTitle";
    CharSequence contentText = "some content goes here";

    Notification notification = new Notification(icon, tickerText, when);
    Intent notificationIntent = new Intent(this, MyClass.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);    
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    mNotificationManager.notify(NOT_ID, notification);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号