我的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);
答案 0 :(得分:9)
我遇到了同样的问题,然后意识到我没有在Manifest中宣布我的新活动。
<activity
android:name=".YourActivityHere">
</activity>
答案 1 :(得分:3)
您需要为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);
它对我有用。
答案 2 :(得分:2)
现在我知道这个问题非常陈旧,但对我来说,解决方法是更改requestCode。显然,当requestCode为0时,它有时不起作用。它在一部手机上对我有用但在另一部手机上没有工作。
答案 3 :(得分: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);
}