我正在开发一个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" />
答案 0 :(得分:0)
int reqCode = Math.abs((int) System.currentTimeMillis());
System.currentTimeMillis()
是long
尝试设置任何常量int
targetIntent
吗?