Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Android ignoring the creation of the Notification?

I have code, and it just does not work, so I ask anyone to help me out. There is very little info on this specific matter.

MainActivity


public class MainActivity extends Activity {
public final int PENDING_INTENT_ID = 8;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button clickity = (Button)findViewById(R.id.alarm_button);
    clickity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar now = Calendar.getInstance();
            now.add(Calendar.SECOND, 20);

            //Create a new PendingIntent used by the Alarm when it activates
            Intent intent = new Intent(v.getContext(), AlarmReciever.class);
            intent.setAction("SOME_AWESOME_TRIGGER_WORD");
            intent.putExtra("info", "This String shows that the info is actually sent correctly");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(v.getContext(), PENDING_INTENT_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT|Intent.FILL_IN_DATA);

            //Then Create the alarm manager to send this pending intent and set the date to go off
            AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), pendingIntent);

        }
    });

}

AlarmReciever (Aware I spelled it wrong but since thats how it is, im going with it)


public class AlarmReciever extends BroadcastReceiver{

@Override
public void onReceive(Context c, Intent arg1) {

    //get a reference to NotificationManager
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) c.getSystemService(ns);

    //Instantiate the notification

    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();
    Notification.Builder builder = new Notification.Builder(c)
                                .setTicker(tickerText)
                                .setWhen(when)
                                .setContentTitle(arg1.getStringExtra("info"))
                                .setContentText("Success!!")
                                .setAutoCancel(true);
    Notification notification = builder.getNotification();
    mNotificationManager.notify(0, notification);//note the first argument, the ID should be unique



}
}

Manifest


<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver 
        android:name="com.testproject.AlarmReciever"
        android:enabled="true"
        android:exported="false" >
        <intent-filter>
    </receiver>

</application>


That should be everything. I am trying to run it as is and its not showing me anything. I am running it on an emulator is that actually matters.

EDIT: When I say it doesn't work, I mean nothing pops up. It runs fine, but the Notification never pops up.

The issue: enter image description here

So the issue is narrowed down to Android just ignoring my Notification. Problem is it doesn't tell me why -_- so I can't fix it. Any experts on the matter see something wrong with my code to call a notification? Does it matter that its on an emulator?

like image 315
Andy Avatar asked Jul 07 '12 01:07

Andy


3 Answers

I ran into the same issue. If you don't specify an icon when creating a "new Notification()", the notification will not appear.

like image 178
Erich Cervantez Avatar answered Oct 27 '22 08:10

Erich Cervantez


Well, lesson learned on Notifications. The reason I was getting the error was because an img MUST be added, if not, it will not show! Everything else I had was basically right with the exception of what Vladimir was graciously able to point out. Posting this here incase others are getting a similar issue just testing out the notifications.

like image 32
Andy Avatar answered Oct 27 '22 08:10

Andy


Intent intent = new Intent(v.getContext(), AlarmReciever.class);
// intent.setAction("SOME_AWESOME_TRIGGER_WORD"); replace to:
intent.setAction("com.testproject.SOME_AWESOME_TRIGGER_WORD");

It's at least for first look

UPD:

long firstTime = SystemClock.elapsedRealtime();

AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, /* INTERVAL IN MS */ 20 * 1000, /* PendingIntent */ intent);
like image 35
Volodymyr Lykhonis Avatar answered Oct 27 '22 06:10

Volodymyr Lykhonis