Skip to content

Instantly share code, notes, and snippets.

@viniciusgati
Created February 1, 2018 23:15
Show Gist options
  • Save viniciusgati/3dc87e02f77fdd9a76501cb17e647d73 to your computer and use it in GitHub Desktop.
Save viniciusgati/3dc87e02f77fdd9a76501cb17e647d73 to your computer and use it in GitHub Desktop.
[source code] Android Development Tutorial - 61 & 62 Notifications
/* API23 WARNING
* This activity was created with API21 when ActioinBarActivity was not depreciated.
* IF you are using API23 or later then you will need to extend AppCompatActivity
*/
package your.package.name
****** activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click for Notification"
android:id="@+id/buckysButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="buckysButtonClicked" />
</RelativeLayout>
***** MainActivity.java
package com.thenewboston.notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends ActionBarActivity {
NotificationCompat.Builder notification;
private static final int uniqueID = 45612;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notification = new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
}
public void buckysButtonClicked(View view){
//Build the notification
notification.setSmallIcon(R.drawable.ic_launcher);
notification.setTicker("This is the ticker");
notification.setWhen(System.currentTimeMillis());
notification.setContentTitle("Here is the title");
notification.setContentText("I am the body text of your notification");
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
//Builds notification and issues it
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(uniqueID, notification.build());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment