简体   繁体   English

当应用程序不在后台或前台时,无法将通知推送到通知管理器

[英]When the app is not in background or foreground, not able push Notification to Notification manager

I am developing a Android app,i want to push notification to NotificationManager if even app is not in background but not able to make it. 我正在开发一个Android应用程序,我想将通知推送到NotificationManager,即使应用程序不在后台但不能成功。

when app is in opened -working. 当应用程序处于打开状态时。

when app is in background -working 当app处于后台工作时

when app is in not in background or foreground -Not working 当应用程序不在后台或前台时 - 不工作

 NotificationManager notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);NotificationCompat.Builder notificationMsg = new NotificationCompat.Builder(ctx);
         notificationMsg.setContentTitle(notification.getTitle());
         notificationMsg.setTicker(notification.getTitle());
         notificationMsg.setPriority(notification.getPriority());
         notificationMsg.setSmallIcon(R.drawable.foo_icon);
         notificationMsg.setStyle(new NotificationCompat.BigTextStyle().bigText(notification.getDescription()));
         notificationMsg.setContentText(notification.getDescription());
         if (notificationIcon != null)
         {
             notificationMsg.setLargeIcon(notificationIcon);
         }
         else
         {
            // notificationMsg.setLargeIcon(R.drawable.foo_icon);
         }
        notificationMsg.setColor(ctx.getResources().getColor(R.color._9c9c9c));
        notificationMsg.setLights(R.color.green_normal,5000,5000);
        notificationMsg.setVibrate(new long[]{100,250,100,500});

        notificationMsg.setAutoCancel(true);
        if (( notification.getGroupName() != null ) &&( notification.getGroupValue() != null))
        {
            notificationMsg.setGroup(notification.getGroupName()+notification.getGroupValue());
        }
        /* Add Actions to Notification  */
        addActionsToNotificationWindow(notificationMsg,notification,ctx);
        int reqCode = Math.abs((int) System.currentTimeMillis());
        PendingIntent contentIntent = PendingIntent.getActivity(ctx, reqCode, targetIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        notificationMsg.setContentIntent(contentIntent);

        /* Display notification*/
        notifyMgr.notify(notification.getId(),notificationMsg.build()); 

I am using above code to notify to Notification Manager and using following permissions in Android Manifest file 我使用上面的代码通知Notification Manager并在Android Manifest文件中使用以下权限

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.foo"
    android:versionCode="1"
    android:versionName="0.1" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="19" />
    <permission
        android:name="com.disha.quickride.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-permission android:name="android.permission.READ_LOGS" />
    <permission
        android:name="com.disha.quickride.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="android.permission.ACCESS_GPS" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.disha.quickride.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="com.disha.quickride.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.SEND" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />   
  1. in int reqCode = Math.abs((int) System.currentTimeMillis()); in int reqCode = Math.abs((int) System.currentTimeMillis());

System.currentTimeMillis() is long System.currentTimeMillis()long

try to set any constant int 尝试设置任何常量int

  1. also , can you show to us targetIntent ? 另外,你能告诉我们targetIntent吗?
  2. DO you generate the notification from service? 你是否从服务中生成通知?
  3. also show your Manifest 也显示你的清单
  4. you can see here how to generate a notification from service http://startandroid.ru/ru/uroki/vse-uroki-spiskom/164-urok-99-service-uvedomlenija-notifications 你可以在这里看到如何从服务生成通知http://startandroid.ru/ru/uroki/vse-uroki-spiskom/164-urok-99-service-uvedomlenija-notifications

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 工作灯:应用程序在前台时无法接收推送通知 - Worklight: App is not able to receive the push notification when in foreground 应用程序在前台时收到的推送通知与应用程序在后台时收到的推送通知不同 - Push notification received while the app is in the foreground is different from push notification received when the app is in background 当应用程序处于前台时发送推送通知 - Push notification is sent when app is in foreground 当应用在后台时推送通知 - Push notification when app is in background 当应用程序在前台时未收到FCM推送通知,但在应用程序在后台时收到 - FCM Push notification didn't receive when app is in foreground but receive when app in background Android 仅当应用程序处于前台时才播放推送通知声音,但当应用程序处于后台时不播放声音 - Android Push notification sound is played only when app is in foreground but not playing sound when app is in background 当应用程序在前台而不是后台时,FCM通知,通知单击 - FCM notification, notification click when app is in foreground not background 离子1信号有一种方法可以知道从背景到前景打开应用程序或启动应用程序时是否有推送通知 - ionic 1 onesignal is there a way to know if there is a push notification when open app from background to foreground or launching app 如果应用程序不在后台或前台,则解析推送通知将不起作用。 - Parse Push Notification does not work if app not in background or foreground. 如何使用IBM Mobilefirst接收到推送通知时知道应用程序是在后台还是在前台 - how to know if app is in background or foreground when push notification is received using IBM Mobilefirst
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM