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

时间:2015-09-04 09:23:20

标签: android notifications

我正在开发一个Android应用程序,我想将通知推送到NotificationManager,即使应用程序不在后台但无法制作它。

当app处于打开状态时。

当app处于后台工作时

当app不在后台或前台时 - 不工作

 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()); 

我使用上面的代码通知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 个答案:

答案 0 :(得分:0)

  1. in int reqCode = Math.abs((int) System.currentTimeMillis());
  2. System.currentTimeMillis()long

    尝试设置任何常量int

    1. 另外,您能向我们展示targetIntent吗?
    2. 您是否从服务生成通知?
    3. 还会显示您的清单
    4. 您可以在此处看到如何从服务生成通知 http://jsfiddle.net/o8u13whg/3/