我正在开发一个Android应用程序,即使应用程序不在后台,我也想将通知推送到NotificationManager,但无法实现。
当应用程序处于打开状态时-正常工作。
当应用程序处于后台时-正常工作。
当应用程序既不在后台也不在前台时-不起作用
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());
我正在使用上述代码来通知通知管理器,并在Android清单文件中使用以下权限。
<?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" />