我正在使用Android应用,并且目前有用户按照设定的时间和日期进行通知。
我的问题是,在我设置时间和日期后,我从手机关闭应用程序。但是当我关闭应用程序时,通知无效。
这是我的广播接收器类`
@Override
public void onReceive(Context context, Intent intent) {
String text=intent.getStringExtra("param");
List<String> content = Arrays.asList(text.split("\\s*,\\s*"));
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent1=new Intent(context.getApplicationContext(),HomeScreen.class);
PendingIntent pIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, intent, 0);
Notification notif=new Notification.Builder(context.getApplicationContext()).setContentTitle(content.get(0))
.setContentText(content.get(1))
.setSound(soundUri)
.setContentIntent(pIntent)
.setSmallIcon(R.mipmap.ic_launcherplannerlogo)
.build();
notif.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notif.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notif.defaults |= Notification.DEFAULT_VIBRATE;
NotificationManager notifMan=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notifMan.notify(0,notif);
}
这是我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jacobabraham13.homeworkapplication">
<!--<receiver android:process=":remote" android:name=".AlarmReceiver"
android:permission="com.google.android.c2dm.permission.SEND"
/>--> <!--android:process=":remote" android:enabled="true"-->
<receiver
android:name=".AlarmReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<category android:name="com.google.android.gcm.demo.app" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.persmission.SET_ALARM" />
<!-- <service android:name=".AlarmReceiver" android:enabled="true" android:exported="false">
<intent-filter> <action android:name="NOTIFICATION_SERVICE" /></intent-filter>
</service>-->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcherplannerlogo"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ApplicationFunc.MainActivityLogIn">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ApplicationFunc.HomeScreen"
android:label="@string/title_activity_home_screen"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".ApplicationFunc.AddHomework" />
<!-- <service android:name=".ApplicationFunc.AlarmReceiver"
android:exported="false" />
<activity android:name=".ApplicationFunc.ChooseTime"></activity>-->
</application>
</manifest>
我在这里做错了什么?
请帮助!!
`
答案 0 :(得分:0)
您应该使用以下内容替换PendingIntent行:
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_NO_CREATE);
因为PendingIntent.FLAG_NO_CREATE标志表示如果描述的PendingIntent尚不存在
答案 1 :(得分:0)
First Check When app is close OnReceive() method is Call or Not
if OnReceive() Method is calling, when app is close. try below Code.
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcherplannerlogo)
.setContentText(content.get(1))
.setContentIntent(pIntent)
.setAutoCancel(true)
.notificationManager.notify(0, mBuilder.build());