我已经使用下面给出的具有特定id的代码以编程方式在android中创建了状态栏通知
Notification notification;
public static NotificationManager myNotificationManager;
public static final int NOTIFICATION_ID = 1;
private static void createStatusBarNotification(Context context,CharSequence NotificationTicket, CharSequence NotificationTitle,CharSequence NotificationContent, int icon) {
myNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
long when = System.currentTimeMillis();
notification = new Notification(icon, NotificationTicket, when);
Intent notificationIntent = new Intent(context, MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notification.setLatestEventInfo(context, NotificationTitle,NotificationContent, contentIntent);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
myNotificationManager.notify(NOTIFICATION_ID, notification);
}
Run Code Online (Sandbox Code Playgroud)
现在我想要的是以编程方式单击应用程序中的按钮来删除此通知.
我怎样才能实现这一目标?
如图所示......
我收到了通知图标(左侧是红色).
但我需要显示黑色箭头所示的应用程序图标
public void notify(View view){
notification.setSmallIcon(R.drawable.ic_stat_name);
notification.setTicker("Welcome to ****");
notification.setWhen(System.currentTimeMillis());
notification.setContentTitle("abcd");
notification.setContentText("abcd");
Intent intent = new Intent(this, home.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(uniqueID, notification.build());
}
Run Code Online (Sandbox Code Playgroud) notifications android android-notifications android-notification-bar android-studio
我在我的应用程序中使用GCM,并且每当收到GCM消息时都使用NotificationManager创建通知.现在一切正常,GCM消息在通知区域正确显示,但是当我点击通知时它应该启动一个活动我的应用程序将显示未发生的消息详细信息.每次我点击通知它都不会启动任何活动,它仍然保持原样.我创建通知的代码是:
private void sendNotification(String msg) {
SharedPreferences prefs = getSharedPreferences(
DataAccessServer.PREFS_NAME, MODE_PRIVATE);
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, WarningDetails.class);
Bundle bundle = new Bundle();
bundle.putString("warning", msg);
bundle.putInt("warningId", NOTIFICATION_ID);
intent.putExtras(bundle);
// The stack builder object will contain an artificial back stack for
// the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the …Run Code Online (Sandbox Code Playgroud) 由于通知中的Android 5.0大图标具有彩色背景:

对于小图标,它是notification(Notification.Builder.setColor(int))的强调颜色.如何将其设置为大图标?它是实际图像的一部分吗?如果是,圆半径应该是多少?
当我使用向量drawable设置通知的小图标时,我得到以下异常:
android.app.RemoteServiceException:从包com.qbes.xxx发布的错误通知:无法创建图标:StatusBarIcon(pkg = com.qbes.xxxuser = 0 id = 0x7f020082 level = 0 visible = true num = 0)
这是我的代码:
mNotificationBuilder = new android.support.v4.app.NotificationCompat.Builder(this)
.setDefaults(android.support.v4.app.NotificationCompat.DEFAULT_LIGHTS)
.setSound(null)
.setSmallIcon(R.drawable.logo_white)
.setColor(getResources().getColor(R.color.colorPrimary))
.setCategory(android.support.v4.app.NotificationCompat.CATEGORY_PROGRESS)
.setContentTitle("Trip in Progress...")
.setAutoCancel(false)
.setProgress(0, 0, progress)
.setOngoing(true)
.setPriority(android.support.v4.app.NotificationCompat.PRIORITY_MAX)
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent);
mNotificationBuilder.setContentText(body);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification note = mNotificationBuilder.build();
mNotificationManager.notify(Constants.NOTIFICATION_ID_Dash, note);
Run Code Online (Sandbox Code Playgroud)
和我build.gradle(仅相关部分):
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.qbes.xxx"
minSdkVersion 16
targetSdkVersion 22
versionCode 720
versionName "0.7.20"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled …Run Code Online (Sandbox Code Playgroud) java android android-appcompat android-notifications android-support-library
Notification.Builder(context)最近已被弃用,其中包含Android O中的通知频道.
问题:
使用后Notification.Builder(context, StringID)代替Notification.Builder(context)我确实收到了我的Android O设备的通知.
但是,在Android 23(M)上尝试后,我没有收到通知.我调试了我的代码,一旦调试器在Android 23(M)上点击Notification.Builder(context,StringID)后面的行就停止执行.
固定:
为解决此问题,我使用if/else条件在Android O设备和其他其他设备之间进行隔离.
我有以下代码片段:
Notification.Builder notificationBuilder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder = new Notification.Builder(mContext,
mContext.getResources().getString(R.string.notification_id_channel));
} else {
notificationBuilder = new Notification.Builder(mContext);
}
Run Code Online (Sandbox Code Playgroud)
Android Studio中的Lint显示以下弃用行:
题:
有没有办法摆脱弃用警告线?
我想在特定时间通知我的应用.每天说我必须在上午7点发出通知,即使该应用程序已关闭.
我怎样才能做到这一点?任何教程?请提及链接.
到目前为止,我已经调整了我的代码ContextCompat.startForegroundService(context, intentService);来启动我的服务.这样,它适用于android <26和android 26(Oreo).
我仍然看到一个区别,在android oreo我没有看到我的自定义前景通知(我只看到"应用程序在后台运行"通知).我还需要调整任何东西吗?
我的服务如下:
public class BaseOverlayService extends Service {
@Override
public void onCreate() {
super.onCreate();
moveToForeground();
}
private void moveToForeground() {
Notification notification = ...;
super.startForeground(NOTIFICATION_ID, notification);
}
}
Run Code Online (Sandbox Code Playgroud)
官方的例子
此示例(https://github.com/googlesamples/android-play-location/blob/master/LocationUpdatesForegroundService/app/src/main/java/com/google/android/gms/location/sample/locationupdatesforegroundservice/LocationUpdatesService.java #L200)显示为评论,我应该使用以下,但startServiceInForeground不存在......
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
NotificationManager mNotificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
mNotificationManager.startServiceInForeground(new Intent(this, BaseOverlayService.class), NOTIFICATION_ID, notification);
} else {
startForeground(NOTIFICATION_ID, notification);
}
Run Code Online (Sandbox Code Playgroud)
编辑
我的通知是这样创建的,它正在使用API <26的数千个Android设备上工作到现在为止:
protected Notification foregroundNotification(int notificationId)
{
boolean paused = MainApp.getPrefs().sidebarServicePaused();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this); …Run Code Online (Sandbox Code Playgroud) android android-notifications foreground-service android-8.0-oreo
我在GCMIntentservice中编写了一个代码,用于向许多用户发送推送通知.我使用NotificationManager,在单击通知时将调用DescriptionActivity类.我还将event_id格式的GCMIntentService发送到DescriptionActivity
protected void onMessage(Context ctx, Intent intent) {
message = intent.getStringExtra("message");
String tempmsg=message;
if(message.contains("You"))
{
String temparray[]=tempmsg.split("=");
event_id=temparray[1];
}
nm= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
intent = new Intent(this, DescriptionActivity.class);
Log.i("the event id in the service is",event_id+"");
intent.putExtra("event_id", event_id);
intent.putExtra("gcmevent",true);
PendingIntent pi = PendingIntent.getActivity(this,0, intent, 0);
String title="Event Notifier";
Notification n = new Notification(R.drawable.defaultimage,message,System.currentTimeMillis());
n.setLatestEventInfo(this, title, message, pi);
n.defaults= Notification.DEFAULT_ALL;
nm.notify(uniqueID,n);
sendGCMIntent(ctx, message);
}
Run Code Online (Sandbox Code Playgroud)
这里我在上面的方法中得到的event_id是正确的,即我总是得到更新的.但是在下面的代码中(DescriptionActivity.java):
intent = getIntent();
final Bundle b = intent.getExtras();
event_id = Integer.parseInt(b.getString("event_id"));
Run Code Online (Sandbox Code Playgroud)
这里的event_id总是"5".无论我把什么放在GCMIntentService类中,我得到的event_id总是5.有人可以指出问题吗?是因为未决意图?如果是,那我该怎么处理呢?
push-notification android-notifications android-notification-bar android-pendingintent
我无法改变这种感觉:再次,Android开发人员提出了一些新的东西,让所有人都不知道他们会如何看待这个功能.
我在谈论Android O中的通知频道.
多年来,我一直在使用兼容性支持库来避免处理特定的平台细节.即:NotificationCompat.
现在,Builder要求我提供一个通知通道ID,这很好,但完全不用我创建这样一个通道.我找不到任何compat支持创建频道.我也找不到合适的方法来在正确的点上创建它们.
文档只是声明它应该"在某处"和"在发出通知时可能不会".但究竟我应该做什么?我讨厌为简单的任务编写特定版本的东西 - 这就是我使用compat库的原因.
有没有人建议如何处理它?当我想要显示通知时,每次创建是否"昂贵"?
java android-notifications android-support-library android-8.0-oreo