简体   繁体   English

如何在单击通知时启动活动?

[英]How to launch an activity when notification is clicked?

I have a strange problem in my app in android. 我的Android应用程序中有一个奇怪的问题。 I have done 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. 我遇到了同样的问题,然后意识到我没有在Manifest中宣布我的新活动。

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

you need set action and category for Intent. 你需要为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: 如果是你的应用程序的入口点,你可以通过xml获得它,就像它:

<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. 现在我知道这个问题非常古老,但对我来说,解决方案是改变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. 显然,当requestCode为0时,它有时不起作用。它在一部手机上对我有用但在另一部手机上无效。

As in the comments: I can't see anything wrong with your code, and suspect your shopsonmap to just not show anything. 在评论中:我看不出你的代码有什么问题,并怀疑你的shopsonmap没有显示任何内容。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM