Why does Android ignore the creation of Notifications?

I have a code and it just doesn't work, so I ask someone to help me. There is very little information on this particular issue.

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 (realizing that I spelled it wrong, but since it is, I'm 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 all. I try to run it as is and it does not show me anything. I run it on an emulator, this is really important.

EDIT: When I say this does not work, I mean that nothing pops up. It works fine, but the notification never appears.

Problem: enter image description here

, , Android . , , -_- . - , ? ?

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

, ,

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);
+2

. " Notification()", .

+22

, . , , , img , , ! , , , , . , , .

+8

, . notificaiton, . , .

0

, Icon. .

.

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
0

All Articles