标签: android-notifications

PendingIntent未在Android 4.3中打开活动

在我的中Service,我使用以下代码在正常运行时打开通知:

private final static NOTIFICATION_ID = 412434;
private void startNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            this);
    builder.setSmallIcon(R.drawable.notification);
    builder.setContentTitle("Running");

    final Intent intent = new Intent(this, MainActivity.class);
    intent.setAction(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    final PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);
    builder.setOngoing(true);
    builder.setAutoCancel(false);

    notification = builder.build();

    startForeground(NOTIFICATION_ID, notification);
}
Run Code Online (Sandbox Code Playgroud)

PendingIntent是打开MainActivity时,通知被窃听.这在我的所有测试设备上运行得非常好,使用Android 2.3.3,2.3.5和Android 4.1.

它不起作用,但是在我的Nexus 7(Android 4.3)上,这根本不起作用.点击通知时没有任何反应.

我错过了这些放在一起的方式有什么变化吗?

android android-intent android-notifications

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

Android多行通知,例如Gmail应用

我正在尝试创建一个多行通知,如Gmail应用程序所示,如下图所示(5个通知分组在一个通知下)

在此输入图像描述

我尝试了各种示例,但似乎只能创建单个通知

   public void createSingleNotification(String title, String messageText, String tickerttext) {
        int icon = R.drawable.notification_icon; // icon from resources
        CharSequence tickerText = tickerttext; // ticker-text
        long when = System.currentTimeMillis(); // notification time
        Context context = getApplicationContext(); // application Context
        CharSequence contentTitle = title; // expanded message title
        CharSequence contentText = messageText; // expanded message text
        Intent notificationIntent = new Intent(this, MainActivity.class);

        Bundle xtra = new Bundle();
        xtra.putString("title", title);
        xtra.putString("message", messageText);

        notificationIntent.putExtras(xtra);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
          notificationIntent, PendingIntent.FLAG_ONE_SHOT
            + PendingIntent.FLAG_UPDATE_CURRENT); …
Run Code Online (Sandbox Code Playgroud)

android android-notifications

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

如何使用RemoteViews更新通知?

我正在使用RemoteViews自定义创建通知,该自定义Service以前台模式运行通知(即,只要用户可以看到通知,服务就会保持活动状态).通知设置为正在进行,因此用户无法将其滑动.

我想更改示例中显示的位图ImageView,包含在远程视图的布局中或更改文本值TextView.远程视图中的布局使用XML布局文件设置.

我的问题是,一旦通知被创建并且对用户可见,如果我调用任何RemoteViews类似于setImageViewResource()更改中Bitmap显示的任何功能ImageView,则更改是不可见的,除非我setImageViewResource()之后打电话给我打电话:

NotificationManager.notify( id, notification );
Run Code Online (Sandbox Code Playgroud)

要么

Service.startForeground(id,notification);
Run Code Online (Sandbox Code Playgroud)

这对我来说听起来不对.我无法相信要RemoteViews在已创建的通知中更新UI,我必须重新初始化通知.如果我对Button通知有控制权,它会在触摸和释放时自行更新.所以必须有一种方法可以做到这一点,但我不知道如何.

这是我的代码,它在我的Service实例中创建通知:

this.notiRemoteViews = new MyRemoteViews(this,this.getApplicationContext().getPackageName(),R.layout.activity_noti1);

Notification.Builder notibuilder = new Notification.Builder(this.getApplicationContext());
notibuilder.setContentTitle("Test");
notibuilder.setContentText("test");
notibuilder.setSmallIcon(R.drawable.icon2);
notibuilder.setOngoing(true);

this.manager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
this.noti = notibuilder.build();
this.noti.contentView = this.notiRemoteViews;
this.noti.bigContentView = this.notiRemoteViews;
this.startForeground(NOTIFICATION_ID, this.noti);
Run Code Online (Sandbox Code Playgroud)

并且"强制"UI更改为通知的功能:

public void updateNotiUI(){
    this.startForeground(NOTIFICATION_ID, this.noti);
}
Run Code Online (Sandbox Code Playgroud)

MyRemoteViews课堂上,如果需要,我这样做是为了对UI进行更改:

this.setImageViewResource(R.id.iconOFF, R.drawable.icon_off2);
this.ptMyService.updateNotiUI();
Run Code Online (Sandbox Code Playgroud)

谁能告诉我更新RemoteViews通知中UI组件的正确方法是什么?

android android-notifications android-remoteview

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

为什么Android 13删除了前台服务通知?

抱歉,这种标题是吸引点击的标题;我想不出更简洁的方式来表达它。

在 Android 13 中,如果用户未授予“危险”POST_NOTIFICATION权限,则前台服务通知不会显示在通知抽屉中。相反,为了让用户看到它,他们必须导航到新的前台服务任务管理器,根据文档

如果用户拒绝通知权限,他们仍然会在前台服务 (FGS) 任务管理器中看到与这些前台服务相关的通知,但不会在通知抽屉中看到它们。

现在我还没有使用过 Android 13 Beta,所以我不知道当前台服务运行时 FGS 任务管理器到底会是什么样子,但我认为强制前台服务发出通知的全部意义在于以便用户知道应用程序何时运行。这甚至会产生安全后果,因为 Android 限制了后台进程与前台进程可以使用危险权限(例如ACCESS_BACKGROUND_LOCATION)执行的操作。此更改本质上允许应用程序使用基于前台的权限,而无需明确通知用户。

那么,他们为什么决定限制前台服务通知呢?我的意思是,首先还有关于添加限制通知的其他讨论。但有人会认为,如果 Android强制您使用通知,那么就不应该有办法绕过它(即从不请求权限POST_NOTIFICATION,甚至只是以编程方式删除权限)。

android android-notifications foreground-service android-13

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

如何实现Notification的弃用方法

我有一个小问题,但不明白如何摆脱这个.

我创建了一个用于提供通知的类,但这些行被标记为已弃用:

...
Notification notification = new Notification(icon, text, time); // deprecated in API level 11
...
notification.setLatestEventInfo(this, title, text, contentIntent); // deprecated in API level 11
...
Run Code Online (Sandbox Code Playgroud)

替代方法是:

...
Notification noti = new Notification.Builder(mContext)
         .setContentTitle("New mail from " + sender.toString())
         .setContentText(subject)
         .setSmallIcon(R.drawable.new_mail)
         .setLargeIcon(aBitmap)
         .build(); // available from API level 11 and onwards
...
Run Code Online (Sandbox Code Playgroud)

我可以编写类似的代码:

if(API_level < 11)
{
...
    Notification notification = new Notification(icon, text, time); // deprecated in API level 11
    ...
    notification.setLatestEventInfo(this, title, text, contentIntent); // deprecated …
Run Code Online (Sandbox Code Playgroud)

android android-notifications

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

在自定义通知中添加按钮操作

我已经制作custom notification并且有一个按钮,我想要执行两个不同的functionalities on notification and button click.我看了很多链接但找不到添加按钮监听器的方法.

谁能帮忙.这是我的代码.非常感谢.

 private void startNotification() {
    Intent intent;
    PendingIntent pIntent;
    RemoteViews remoteViews = new RemoteViews(getPackageName(),
            R.layout.mynotification);

    Context context = getApplicationContext();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher).setContent(
            remoteViews);

    if (hasFlash) {
        intent = new Intent(context, FlashLight.class);
        pIntent = PendingIntent.getActivity(context, 1, intent, 0);
    } else {
        intent = new Intent(context, BlankWhiteActivity.class);
        pIntent = PendingIntent.getActivity(context, 1, intent, 0);
    }
    builder.setContentIntent(pIntent);
    NotificationManager mNotificationManager = (NotificationManager)      getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notif = builder.setContentTitle("Flashlight")
            .setContentText("Lighten your world!!!").build();
    mNotificationManager.notify(1, …
Run Code Online (Sandbox Code Playgroud)

android android-intent android-layout android-notifications android-fragments

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

如何在android中按下通知时打开片段页面

我在通知栏中按通知时试图打开一个片段.我的app结构是:

  • 带导航抽屉菜单的基本活动
  • 从菜单打开的一些片段

    b.setOnClickListener(new OnClickListener() {
    
            @SuppressWarnings({ "deprecation", "static-access" })
            public void onClick(View v) {
    
            w_nm=(NotificationManager) getActivity().getSystemService(getActivity().NOTIFICATION_SERVICE);
    
             Notification notify=new Notification(R.drawable.notnificationlogo,waternoti,System.currentTimeMillis());
    
             Intent notificationIntent = new Intent(getActivity(), Abc.class);
    
    
    
             PendingIntent pending=PendingIntent.getActivity(getActivity(), 0,notificationIntent, 0);
    
    
             notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                     | Intent.FLAG_ACTIVITY_SINGLE_TOP );
    
            notify.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
    
               notify.setLatestEventInfo(getActivity(),waternoti,waternoti1, pending);
    
             w_nm.notify(0, notify);
    
    Run Code Online (Sandbox Code Playgroud)

谁能告诉我如何链接下一个片段页面(当前代码在扩展片段的类中)

android android-notifications android-fragments android-pendingintent

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

如何在Android中安排通知

我正试图在将来设置通知.我有创建通知的代码,但我找不到安排它的选项.

我该如何安排通知?

java notifications android android-notifications

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

在Android 8.1 API 27上,通知不会显示

我在Android 8.1 API 27上获得了Toast:

包"my_package_name"的开发者警告
无法在...上发布通知

Logcat包含下一个字符串:

通知:对于卷控制以外的操作,不推荐使用流类型

W/Notification:请参阅setSound()的文档,了解如何使用android.media.AudioAttributes来限定播放用例

E/NotificationService:找不到pkg = my_package_name的频道

Toast和Logcat中的完整信息可以帮助解决此问题.

notifications android android-notifications android-8.0-oreo

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

当应用在后台或未运行时,推送通知无法正常工作

我正在使用Firebase Cloud Messaging发送推送通知.

这是我的FirebaseMessageService:

public class FireBaseMessageService extends FirebaseMessagingService {


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e("TAG", "From: " + remoteMessage.getFrom());
    Log.e("TAG", "Notification Message Body: " + remoteMessage.getData().get("CardName")+"  :  "+remoteMessage.getData().get("CardCode"));
    sendNotification(remoteMessage.getNotification().getBody());
}


private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, StartActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher_final)
            .setContentTitle("Notification")
            .setContentText(messageBody)
            .setTicker("Test")
            .setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID …
Run Code Online (Sandbox Code Playgroud)

service android push-notification android-notifications firebase-cloud-messaging

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