簡體   English   中英

如何在單擊通知時啟動活動?

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

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

我遇到了同樣的問題,然后意識到我沒有在Manifest中宣布我的新活動。

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

你需要為Intent設置動作和類別。

如果此活動不是應用程序的入口點:

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

這個對我有用。

現在我知道這個問題非常古老,但對我來說,解決方案是改變requestCode。 顯然,當requestCode為0時,它有時不起作用。它在一部手機上對我有用但在另一部手機上無效。

在評論中:我看不出你的代碼有什么問題,並懷疑你的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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM