0

I am working on an Android app, and have currently have notifications working on a set time and date by the user.

My issue is that after I set the time and date, and I close the app from the phone. But the notification does not work when I close the app.

This is my Broadcast Receiver class`

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



}

This is my manifest file

<?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>

What am i doing wrong here?

Please help!!

`

Jacob Abraham
  • 41
  • 1
  • 3

2 Answers2

0

you should replace the PendingIntent line with the following:

PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_NO_CREATE);

since PendingIntent.FLAG_NO_CREATE Flag indicates that if the described PendingIntent does not already exist

Khalid Taha
  • 3,183
  • 5
  • 27
  • 43
  • what do you mean by does not work ? does not appear in the notification bar or crashes after opening it ? – Khalid Taha Sep 06 '16 at 03:38
  • The notification does not appear at the set time. Like i said above if I DONT close the app and leave it running in the background then it works – Jacob Abraham Sep 06 '16 at 03:39
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());
Krunal Patel
  • 61
  • 3
  • 12
  • How would i check? I cant track whats going on Android Studio as soon as I close the application. – Jacob Abraham Sep 07 '16 at 00:13
  • Please Refer this link http://stackoverflow.com/questions/39480931/error-broadcast-intent-callback-result-cancelled-forintent-act-com-google-and/39492561#39492561 – Krunal Patel Oct 25 '16 at 06:45