5

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?

Andy
  • 10,553
  • 21
  • 75
  • 125

5 Answers5

22

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

Erich Cervantez
  • 281
  • 3
  • 3
  • 1
    Haha, yea. I wish the docs mention that. But hey, trial and error right. Now other people know :) – Andy Nov 03 '12 at 22:47
8

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.

Andy
  • 10,553
  • 21
  • 75
  • 125
2
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);
Volodymyr Lykhonis
  • 2,936
  • 2
  • 17
  • 13
  • If I take that away, how should I change my manifest file's `` tag? – Andy Jul 07 '12 at 02:05
  • You actually can remove `intent.setAction(..)` from code + remove `` section at all from android manifest. – Volodymyr Lykhonis Jul 07 '12 at 02:07
  • I just did. Still doesn't work. Thanks for pointing it out though. Its a start. – Andy Jul 07 '12 at 02:07
  • Could you please specify, what exactly does not work? Does not get `onReceive` call or there is an exception? You can try to write `Log.d("TAG", "onReceive");` to `onReceive` to make sure it has reached this point in code. To see this message just run `adb logcat` – Volodymyr Lykhonis Jul 07 '12 at 02:09
  • No nothing shows up. It runs fine. But it doesn't do anything when I click the Button. Its suppose to call an alarm in 20 seconds. But actually I will add the error checks. SOrry for not providing that. Give me a little to run them and make sure `onReceive` actually runs. – Andy Jul 07 '12 at 02:11
  • I added what looks like is the issue. Thank you so much for helping me get to that. So it looks like its the image argument, which I put as 0 because I do not want an image. – Andy Jul 07 '12 at 02:18
  • Not sure what's wrong, but small details about `Deprecated` method, use `builder.build();` instead of `builder.getNotification();` – Volodymyr Lykhonis Jul 07 '12 at 02:33
  • But eclipse is not recognizing `build()` a method that is part of `Notification.Builder` for some reason. Like if I try and change that it won't let me compile. – Andy Jul 07 '12 at 02:37
0

This is because, you are not setting the notification icon. If you set notificaiton icon it should work. Log itself says that you can't send a notification without an image.

Maddy
  • 316
  • 1
  • 6
  • 14
0

Seems like you forgot to set Icon.. You need to atleast set a default icon..

Try this..

Notification.Builder builder = new Notification.Builder(c)
                         .setTicker(tickerText)
                         .setWhen(when)
                         .setContentTitle(arg1.getStringExtra("info"))
                         .setContentText("Success!!")
                         .setAutoCancel(true)
                         .setSmallIcon(R.drawable.ic_launcher); //<--- this one
Rafique Mohammed
  • 3,666
  • 2
  • 38
  • 43