3
我正在開發一個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" />
什麼樣的通知,這是一個通知? GCM ?? 發佈更多相關的代碼。 –
使用Mqtt推送通知 –
添加用於服務聲明的服務代碼和AndroidManifest代碼。 –