Как запустить действие при нажатии на уведомление? - PullRequest
4 голосов
/ 14 мая 2011

У меня странная проблема в моем приложении в Android.Я сделал уведомление, и я хочу начать новое действие при нажатии на уведомление.Проблема в том, что когда я нажимаю на уведомление, ничего не происходит, и я понятия не имею, где проблема?Кто-нибудь может мне помочь? Вот мой код:

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); 

Ответы [ 4 ]

9 голосов
/ 03 марта 2012

У меня была такая же проблема, а потом я понял, что я не объявил свою новую деятельность в Манифесте.

<activity
        android:name=".YourActivityHere">
        </activity>
3 голосов
/ 19 мая 2015

вам нужно установить действие и категорию для намерения.

если это действие не является точкой входа в приложение:

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);

и если это точка входа вашего приложения, у вас есть 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>

так, так, нужно только добавить это:

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);

это работает для меня.

2 голосов
/ 04 мая 2014

Теперь я знаю, что этот вопрос ОЧЕНЬ старый, но для меня было решение изменить код запроса. Очевидно, иногда он не работает, когда requestCode равен 0. Он работал для меня на одном телефоне, но не работал на другом.

0 голосов
/ 14 мая 2011

Как и в комментариях: я не вижу ничего плохого в вашем коде и подозреваю, что ваш shopsonmap просто ничего не показывает. Ниже код, который я использую и который работает.

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);
}
...