我刚刚开始处理通知,现在我尝试删除通知,并在通知中心点击通知后启动应用程序.
我尝试使用以下代码:
import android.app.NotificationManager;
public class ExpandNotification {
private int NOTIFICATION = 546;
private NotificationManager mNM;
public void onCreate() {
mNM.cancel(NOTIFICATION);
setContentView(R.layout.activity_on);
//Toast.makeText(this, "stopped service", Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)
我觉得这个代码在点击时执行其他类?
PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, new Intent(this, ExpandNotification.class), 0);
Run Code Online (Sandbox Code Playgroud)
但是,通知不会消失,应用程序也不会启动.但我能够向左或向右滑动它以移除它但这不是我想要的..
010*_*101 106
使用Notification.Builder或NotificationCompat.Builder调用setAutoCancel(true)Builder实例获得相同的效果.
inp*_*put 81
使用标志 Notification.FLAG_AUTO_CANCEL
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
// Cancel the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Run Code Online (Sandbox Code Playgroud)
并启动应用程序:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create a new intent which will be fired if you click on the notification
Intent intent = new Intent(context, App.class);
// Attach the intent to a pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)
这个答案太晚了,但特别是我写下面的解决方案,因为通知构造函数变得弃用所以使用构建器使用通知,如下所示:
**.setAutoCancel(true)** is used to remove notification on click
Run Code Online (Sandbox Code Playgroud)
并且整个通知如下:
private void makeNotification(String title,String msg){
Intent resultIntent = new Intent(this, MasterActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentIntent(resultPendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(msg);
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
Run Code Online (Sandbox Code Playgroud)
使用标题和消息调用此方法可以获得完美的通知.
小智 5
您可以直接.setAutoCancel(true)将此行添加到代码中,以删除点击通知。
必须将其添加到您的构建器变量中。
例子:
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("Notification")
.setContentText("Hello")
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47249 次 |
| 最近记录: |