标签: android-notifications

当应用未运行时,Android通知未显示其内容

这是我有趣的问题.当应用未运行时,来自GCM的Android通知未显示标题和内容(显示应用名称,并在单击时打开MainActivity).

但是当应用程序打开时,它会成功显示标题和内容.可能是什么问题?它运行没有问题,我没有改变任何东西.

表现:

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="com.package.xxx.permission.C2D_MESSAGE" />
    <permission android:name="com.package.xxx.permission.C2D_MESSAGE" android:protectionLevel="signature" />
Run Code Online (Sandbox Code Playgroud)

    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.package.xxx" />
        </intent-filter>
    </receiver>

    <service
        android:name=".Service.GcmService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
Run Code Online (Sandbox Code Playgroud)

GcmService.java:

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import …
Run Code Online (Sandbox Code Playgroud)

java android android-notifications google-cloud-messaging

22
推荐指数
2
解决办法
3163
查看次数

从FCM onMessageReceived方法获取RemoteMessage的值

我有迁移gcm to fcm推送通知消息.但我如何从RemoteMessage获取捆绑数据收到onMesssageReceived方法.

Old GCM give bundle data onMessageReceiced method but in FCM there is RemoteMessage data.
Run Code Online (Sandbox Code Playgroud)

所以请告诉我如何解析remotemessage获取所有通知值.

我的PAYROL

{
"collapse_key":"score_update",
"priority":"high",
"content_available":true,
"time_to_live":108,
"delay_while_idle":true,
"data": 
{ 
    "message": "Message for new task",
    "time": "6/27/2016 5:24:28 PM"
},
"notification": {
    "sound": "simpleSound.wav",
    "badge": "6",
    "title": "Test app",
    "icon": "myicon",
    "body": "hello 6 app",
    "notification_id" : "1140",
    "notification_type" : 1,
    "notification_message" : "TEST MESSAGE",
    "notification_title" : "APP"
  },
"registration_ids": ["cRz9SJ-gGuo:APA91bFJPX7_d07AR7zY6m9khQro81GmSX-7iXPUaHqqcOT0xNTVsOZ4M1aPtoVloLNq71-aWrMCpIDmX4NhMeDIc08txi6Vc1mht56MItuVDdA4VWrnN2iDwCE8k69-V8eUVeK5ISer"
]
}
Run Code Online (Sandbox Code Playgroud)

java android android-notifications google-cloud-messaging firebase-cloud-messaging

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

如何在点击后正确清除所有通知?

我在通知栏上发送了一些通知,我希望在点击其中一个通知时清除所有通知.现在我通过使用Flag逐个清除.我知道notificationManager.cancelAll()可以清除所有通知但我应该放在哪里,以便一旦点击通知之一我就可以触发.

 private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name); 
    Intent notificationIntent = new Intent(context, MainActivity.class);

    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(msgid, notification);  
    //notificationManager.cancelAll(); //i wan to clear all when the notification …
Run Code Online (Sandbox Code Playgroud)

android notificationmanager android-notifications

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

全屏通知仅显示为抬头

我已经用这个问题撞了桌子3天了,请告诉我我误入歧途的地方.

当我收到传入的VoIP电话时,我正在尝试显示全屏通知,就像PhoneApp那样.如果用户处于身临其境的活动中,我可以抬起头来.但是,我只收到抬头通知,并且从未获得全屏.我也需要通知忽略锁定屏幕.

这是我在做的事情:

    String callJson = call.toJson(uber).toString();
    uber.requestWakeState(UberVoice.WAKE_STATE_FULL);

    final Notification.Builder builder = new Notification.Builder(uber);
    builder.setSmallIcon(R.drawable.switch_notification);
    if (image != null) {
        builder.setLargeIcon(image);
    }
    builder.setOngoing(true);

    PendingIntent inCallPendingIntent =
            PendingIntent.getActivity(uber, 234,
                    UberVoice.createInCallIntent(), 0);
    builder.setContentIntent(inCallPendingIntent);

    builder.setContentText(body);
    builder.setUsesChronometer(false);

    builder.setContentTitle(title);

    builder.setCategory(Notification.CATEGORY_CALL);
    builder.setVisibility(Notification.VISIBILITY_PUBLIC);

    builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE), AudioManager.STREAM_RING);
    builder.setVibrate(new long[]{500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500});

    builder.setPriority(Notification.PRIORITY_MAX);
    builder.setTicker(title);
    builder.setFullScreenIntent(inCallPendingIntent, true);

    builder.addAction(R.drawable.hangup_action, "Reject", CallService.getPendingIntent(uber, callJson, CallService.REJECT));
    builder.addAction(R.drawable.answer_action, "Answer", CallService.getPendingIntent(uber, callJson, CallService.ANSWER));

    NotificationManager notificationManager = (NotificationManager) uber.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = builder.build();
    notificationManager.notify(INCOMING_CALL_NOTIFICATION_ID, notification);
Run Code Online (Sandbox Code Playgroud)

这是creatCallIntent代码:

public …
Run Code Online (Sandbox Code Playgroud)

android android-notifications android-fullscreen

21
推荐指数
2
解决办法
4311
查看次数

java.lang.NullPointerException:尝试在启动通知时调用接口方法'java.util.Iterator java.lang.Iterable.iterator()'

我注意到我的一些用户正在获得此异常.我不知道如何重现它,我只有关于Crashlytics的报告.似乎深入Google的代码.在使用此代码的数千人中,只有39人有例外.

知道什么可能是错的吗?

Fatal Exception: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.lang.Iterable.iterator()' on a null object reference
       at android.app.ApplicationPackageManager.getUserIfProfile(ApplicationPackageManager.java:2141)
       at android.app.ApplicationPackageManager.getUserBadgeForDensity(ApplicationPackageManager.java:997)
       at android.app.Notification$Builder.getProfileBadgeDrawable(Notification.java:2877)
       at android.app.Notification$Builder.hasThreeLines(Notification.java:3092)
       at android.app.Notification$Builder.build(Notification.java:3646)
       at android.support.v4.app.NotificationCompatApi21$Builder.build(NotificationCompatApi21.java:136)
       at android.support.v7.app.NotificationCompat$LollipopExtender.build(NotificationCompat.java:504)
       at android.support.v4.app.NotificationCompat$NotificationCompatImplApi21.build(NotificationCompat.java:835)
       at android.support.v4.app.NotificationCompat$Builder.build(NotificationCompat.java:1752)
       at mycode.startNotification(mycode.java:361)
Run Code Online (Sandbox Code Playgroud)

谢谢.

android android-notifications

21
推荐指数
1
解决办法
7037
查看次数

Android牛轧糖中的通知中未显示图标

我研究了这个,发现它addAction (int icon, CharSequence title, PendingIntent intent)已被弃用,所以我用过addAction (Notification.Action action).在这两种情况下,都无法看到图标.

NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_share, "", pendingIntent).build();

notificationBuilder.addAction(action);
Run Code Online (Sandbox Code Playgroud)

文本似乎工作,但我把它留空了,因此在主图像下面有一个空的空间,其中应该显示图标

android android-notifications android-7.1-nougat

20
推荐指数
1
解决办法
6262
查看次数

W/FirebaseMessaging:AndroidManifest 中缺少默认通知通道元数据。将使用默认值

我们的应用程序现在有 targetSdkVersion 26 ( Android 8 ) 并且应用程序使用 FCM 推送通知。当应用程序处于前台状态时,推送通知运行良好。当应用程序不在前台状态时单击通知时会出现此问题。我刚刚在清单中添加了元数据,但仍然出现相同的错误。

AndroidManifest.xml

<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel"
    android:value="@string/default_notification_channel_id"/>
Run Code Online (Sandbox Code Playgroud)

MyFirebaseMessagingService.java

/**
 * Create and show a simple notification containing the received FCM message.
 */
private void sendNotification(NotificationModel notificationModel) 
{
    Intent intent;
    if (notificationModel.getAppLink() != null) 
    {
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(notificationModel.getAppLink()));
    } else 
    {
        intent = new Intent(this, NoticeActivity.class);
    }

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);

    if (notificationModel.getAppLink() != null) …
Run Code Online (Sandbox Code Playgroud)

push-notification android-notifications firebase-cloud-messaging android-studio-3.0 android-8.0-oreo

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

自定义通知声音在奥利奥无效

Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.notification_mp3);
            mBuilder.setSound(sound);
Run Code Online (Sandbox Code Playgroud)

我已将mp3(notification_mp3.mp3)文件复制到res文件夹中的raw文件夹中.当通知被触发时,它产生给定的mp3声音到Android Nougat但在Android Oreo中默认声音.我曾推荐过许多网站,但在Android Oreo上没有任何效果.我没有发现Android Docs中有关Android O及以上版本的通知声音的任何更改.应该做些什么更改才能使此代码在Android O中运行?

android push-notification android-notifications

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

禁用通知权限后启动前台服务导致崩溃(Android 13)

因此,我一直在尝试新的 Android 模拟器(即 Android 13 或 Android Tiramisu,API 33),并且在应用程序上,我需要一个前台服务。

该应用程序的目标 SDK 目前为 33。

由于通知要求,我已将其添加android.permission.POST_NOTIFICATIONS到清单中。而且,因为它也是运行时权限,所以我在打开应用程序后请求权限。

如果用户拒绝该权限,但在使用 启动前台服务后尝试执行涉及前台服务的任务startForegroundService,则在从我的服务调用时startForeground,我会崩溃:

android.app.RemoteServiceException$CannotPostForegroundServiceNotificationException: Bad notification for startForeground
        at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:1983)
        at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2242)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7898)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Run Code Online (Sandbox Code Playgroud)

请注意异常名称:CannotPostForegroundServiceNotificationException。显然,系统有效地阻止了我发布前台服务通知。这种情况发生在startForeground使用有效通知进行调用之后(无论如何,直到 API 32,并且我没有看到 API 32 和 API 33 之间用于构造通知本身的更改,此外setOngoing(true)这是我已经在做的事情)。

因此,我检查了是否可以使用NotificationManager.areNotificationsEnabled(). 如果用户按预期拒绝该权限,则返回 false。代码现在如下所示:

if (mNotificationManager.areNotificationsEnabled())
    startForeground(123, mNotificationBuilder.build())
Run Code Online (Sandbox Code Playgroud)

并且,正如预期的那样,startForeground没有被调用。但是,需要执行的任务可能很长(可能大约2分钟)并且必须在后台执行,这不能在作业中或通过 执行WorkManager,并且如果不调用startForeground …

android android-notifications android-permissions android-13

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

无法在HUAWEI设备上展开RemoteViews:MediaSessionCompat和NotificationCompat.MediaStyle

当我尝试在运行Android 5.0.1的HUAWEI P8 lite设备上显示通知时,我的应用程序崩溃(它在Nexus和Samsung设备上运行正常).我的大部分代码都来自Ian Lake以正确方式播放的视频媒体播放(Big Android BBQ 2015).我的所有代码都在Android服务中.如果我删除代码:

builder.setStyle(new android.support.v7.app.NotificationCompat.MediaStyle()
            .setShowActionsInCompactView(0, 1)
            .setMediaSession(mMediaSessionCompat.getSessionToken()).setShowCancelButton(true)
            .setCancelButtonIntent(MediaStyleHelper.getActionIntent(this, KeyEvent.KEYCODE_MEDIA_STOP)));
Run Code Online (Sandbox Code Playgroud)

然后应用程序不会崩溃,并显示带有图像和标题的通知.但缺少媒体操作按钮.

堆栈跟踪:

FATAL EXCEPTION: 
main Process: com.app.debug, PID: 21600 android.app.RemoteServiceException: 
Bad notification posted from package com.app.demo.debug: Couldn't expand RemoteViews for: 
StatusBarNotification(pkg=com.app.demo.debug user=UserHandle{0} id=1344663 tag=null score=0 key=0|com.app.demo.debug|1344663|null|10121: Notification(pri=0 contentView=com.app.demo.debug/0x109007f vibrate=null sound=null defaults=0x0 flags=0x62 color=0x00000000 category=transport actions=2 vis=PUBLIC))
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1534)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5538)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Run Code Online (Sandbox Code Playgroud)

码:

private void setUpAsForeground(Book book) 
{

    updateMetaData(book);
    startForeground(NOTIFICATION_ID, setupAudioNotification());
}

public void …
Run Code Online (Sandbox Code Playgroud)

android android-notifications android-mediasession

19
推荐指数
1
解决办法
1463
查看次数