标签: android-notifications

单挑通知 - Android Lollipop

我试图展示通知型的单挑,但我不能.我尝试了什么

final Notification.Builder notif = new Builder(getApplicationContext())
    .setContentTitle(getString(R.string.title))
    .setContentText(getString(R.string.text))
//  .setTicker(getString(R.string.tick)) removed, seems to not show at all
//  .setWhen(System.currentTimeMillis()) removed, match default
//  .setContentIntent(contentIntent) removed, I don't neet it
    .setColor(Color.parseColor(getString(R.color.yellow))) //ok
    .setSmallIcon(R.drawable.ic_small) //ok
    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
//  .setCategory(Notification.CATEGORY_CALL) does not seem to make a difference
    .setPriority(Notification.PRIORITY_MAX); //does not seem to make a difference
//  .setVisibility(Notification.VISIBILITY_PRIVATE); //does not seem to make a difference

mNotificationManager.notify(Constants.NOTIFICATION_ID, notif.build());
Run Code Online (Sandbox Code Playgroud)

通知仅显示为栏中的图标.我使用的是API 21 API 21仿真器(不要使用L预览)我曾尝试:
android:Theme.Holo.NoActionBar,
android:Theme.Holo.NoActionBar.Fullscreen
NotificationCompat.Builder

SDK示例不可用.有谁知道怎么做?

我通过添加:

.setDefaults(Notification.DEFAULT_VIBRATE)
Run Code Online (Sandbox Code Playgroud)

这是最好的方法吗?

android android-notifications android-5.0-lollipop

48
推荐指数
4
解决办法
5万
查看次数

单击Android通知操作不会关闭通知抽屉

我正在使用NotificationCompat库向系统栏添加通知.此通知有两个操作按钮.此外,AutoCancel()Notification上的属性设置为true.

单击操作按钮,系统配置为启动IntentService调用NotificationManager.cancel(NOTIFICATION_ID),然后在新任务中启动活动.
问题是虽然此调用会从托盘中删除通知,但它不会折叠抽屉.被叫活动在抽屉后面绘制.

除了取消通知之外,有人可以详细说明关闭抽奖需要什么特殊代码吗?

谢谢.

android android-intent android-notifications

47
推荐指数
3
解决办法
1万
查看次数

Firebase FCM通知click_action有效内容

当用户在后台点击通知时,我正在尝试打开特定活动.从Docs中,我已经知道必须在有效负载中添加click_action,并在App中使用intent过滤来处理它.但是,如何通过Firebase控制台在Firebase通知中添加click_action?我也对任何其他工作开放.提前致谢.

android android-notifications firebase-cloud-messaging firebase-notifications

46
推荐指数
6
解决办法
10万
查看次数

蜂窝通知 - 如何将largeIcon设置为正确的大小?

我发现自己好奇为什么Notification.Builder上的setLargeIcon方法只接受一个Bitmap,没有重载来提供资源id.也许这是出于性能原因而做的,但似乎很奇怪,因为setSmallIcon确实接受了res drawable id.

Notification.Builder builder = new Notification.Builder(application);
// ....
builder.setLargeIcon(iconBitmap);  // Requires a Bitmap
builder.setSmallIcon(iconResId);   // Requires a drawable resource ID
Notification notification = builder.getNotification();
Run Code Online (Sandbox Code Playgroud)

遗憾的是,提供的位图未在通知中缩放,因此需要为通知视图提供完全正确大小的位图.

假设我需要提供largeIcon位图的xhdpi,hdpi,mdpi和ldpi版本,它们需要具有哪些大小?我可以在文档中看到没有提及,或者在浏览更广泛的网络之后.

android android-notifications android-3.0-honeycomb

44
推荐指数
2
解决办法
2万
查看次数

更新通知文本,而不是整个通知

序幕

我正在尝试在通知上添加计时器.计时器是一项服务.每一行调用此行(继续Thread是一个"running"布尔值,timeString是精心设计的String,显示时间):

NotificationChrono.updateNotification(getApplicationContext(), continueThread, 
NOTIF_ID, timeString, "Chronometer", notificationManager);
Run Code Online (Sandbox Code Playgroud)

这是NotificationChrono类:

public class NotificationChrono {

    static public void updateNotification(Context context, boolean running,
        int id, String title, String text,
        NotificationManager notificationManager) {

        Intent stopIntent = new Intent("com.corsalini.david.barcalc.STOP");
        PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context,
            0, stopIntent, 0);

    Intent startIntent = new Intent(
            "com.corsalini.david.barcalc.STARTPAUSE");
    PendingIntent startPendingIntent = PendingIntent.getBroadcast(context,
            0, startIntent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context)

            .setContentText(context.getString(R.string.notif_text))

            .setContentTitle(title)

            .setSmallIcon(R.drawable.ic_action_alarm_2)

            .setAutoCancel(false)

            .setOngoing(running)

            .setOnlyAlertOnce(true)

            .setContentIntent(
                    PendingIntent.getActivity(context, 10, new Intent(
                            context, FrontActivity.class)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), 0))
            .addAction(
                    running ? R.drawable.ic_action_pause
                            : …
Run Code Online (Sandbox Code Playgroud)

android android-notifications

44
推荐指数
3
解决办法
4万
查看次数

在通知Android中加载来自网址的图片

在我的Android应用程序中,我想动态设置将从URL加载的通知图标.为此,我使用setLargeIcon了NotificationBuilder的属性receiver.我提到了许多链接并尝试了各种解决方案,但无法获得所需的输出.虽然我从url下载了该图像并在通知中设置了该位图,但它并未显示.相反,它将setSmallIcon图像显示为大图标.我不知道我哪里错了.我在这里发布我的代码.请帮我解决这个问题.谢谢.

码:

@SuppressLint("NewApi")
public class C2DMMessageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
            Log.e("C2DM", "received message");
            final String fullName = intent.getStringExtra("message");
            final String payload1 = intent.getStringExtra("message1");
            final String payload2 = intent.getStringExtra("message2");
            final String userImage = intent.getStringExtra("userImage");

            Log.e("userImage Url :", userImage); //it shows correct url

            new sendNotification(context)
                    .execute(fullName, payload1, userImage);
        }
    }

private class sendNotification extends AsyncTask<String, Void, Bitmap> {

        Context ctx;
        String message; …
Run Code Online (Sandbox Code Playgroud)

android bitmap android-notifications android-notification-bar google-cloud-messaging

44
推荐指数
6
解决办法
5万
查看次数

android - 发布错误通知 - 无法展开RemoteViews:StatusBarNotification

我试图从IntentService在通知区域中发布带有自定义视图的通知,并获取IntentService错误.

这是我在做的事情Couldn't expand RemoteView:

mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
icon = R.drawable.icon;
tickerText = "data upload in progress";
contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notiflayout);
contentView.setImageViewResource(R.id.image, R.drawable.icon);
contentView.setTextViewText(R.id.text, "Hello");
contentView.setProgressBar(R.id.progressBar, 100, 10, false);
whatNext = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), starterActivity.class), 0);
notification = new Notification(icon, tickerText, System.currentTimeMillis());
notification.contentView = contentView;
notification.contentIntent = whatNext;
Run Code Online (Sandbox Code Playgroud)

我打电话onCreate()notify(),并取消通知onHandleIntent().

我已经验证此代码可以在一个独立的应用程序中运行,该应用程序没有onDestroy().这样做IntentService是以某种方式给予麻烦.

有人可以解释一下我做错了什么吗?

谢谢!

java android android-service android-notifications

43
推荐指数
5
解决办法
4万
查看次数

来自通知的意图没有额外内容

这似乎是一个常见的问题,我经历了我已经找到的所有相关问题: 活动没有获得新意图,为什么在android通知意图中没有发送额外数据(整数)?,Notification传递旧的Intent Extras,不能为通知中的意图,android挂起意图通知问题添加额外内容 ; 但仍然无法弄清楚这一点.

问题是一样的.我设置了一个带有PendingIntent的通知,其中包含一些额外的信息,而我却没有在另一方面得到它.

以下是生成通知的代码:

Notification notification = new Notification(R.drawable.icon, getResources().getString(R.string.notification_ticker), System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;

Intent start_test = new Intent(this, MyActivity.class);
start_test.putExtra("start_test", true);
start_test.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent pi = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), start_test, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, getResources().getString(R.string.notification_title), getResources().getString(R.string.notification_content, expired), pi);
nm.notify(NOTIFICATION_ID, notification);
Run Code Online (Sandbox Code Playgroud)

另一方面:

boolean start_test=getIntent().getBooleanExtra("start_test", false);
Run Code Online (Sandbox Code Playgroud)

捆绑实际上不存在(getExtras()返回null).

我尝试了不同的标志为PendingIntent.getActivity(FLAG_UPDATE_CURRENT,FLAG_CANCEL_CURRENT,FLAG_ONE_SHOT),没有这些帮助的.

如代码所示,我使用getCurrentMillis来确保requestID发生变化....

还发现在MyActivity中,onCreateonNewIntent没有被调用.只是onResume.即使FLAG_ACTIVITY_NEW_TASK设定......?

我错过了一些非常简单的事吗?

android android-intent android-notifications

43
推荐指数
3
解决办法
3万
查看次数

GCM注册ID是否过期?

我知道C2DM注册过期,您应该定期刷新注册ID.这是GCM的情况吗?通过查看Android GCM指南中的以下代码(如下所示),看起来你只做了一次并且不需要刷新,但我没有看到明确写在任何地方,所以我只是想检查.

final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
  GCMRegistrar.register(this, SENDER_ID);
} else {
  Log.v(TAG, "Already registered");
}
Run Code Online (Sandbox Code Playgroud)

android android-notifications google-cloud-messaging

43
推荐指数
2
解决办法
4万
查看次数

可以从工作线程调用NoticationManager.notify()吗?

我的问题更多的是关于什么是好的做法而不是可能的做法:

  • NoticationManager.notify()从工作线程调用是一件好事吗?
  • 系统是否在UI线程中执行它?

我总是试着记住,关于UI的东西应该在UI线程中执行,其余的在工作线程中执行,如Android doc关于进程和线程的建议:

此外,Andoid UI工具包不是线程安全的.因此,您不能从工作线程操纵UI - 您必须从UI线程对您的用户界面进行所有操作.因此,Android的单线程模型只有两个规则:

  • 不要阻止UI线程
  • 不要从UI线程外部访问Android UI工具包

然而,我对Android doc本身给出的一个例子感到惊讶(关于显示Notifications的进展),其中正在进行的通知进度直接从工作线程更新:

mNotifyManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Picture Download")
    .setContentText("Download in progress")
    .setSmallIcon(R.drawable.ic_notification);
// Start a lengthy operation in a background thread
new Thread(
    new Runnable() {
        @Override
        public void run() {
            int incr;
            // Do the "lengthy" operation 20 times
            for (incr = 0; incr <= 100; incr+=5) {
                    // Sets the progress indicator to a max value, the …
Run Code Online (Sandbox Code Playgroud)

notifications multithreading android android-notifications

43
推荐指数
1
解决办法
8040
查看次数