Android Code: Create Phone Notification

Android Code: Create Phone Notification

Thanks for reading this post, it will be on a simple task to create a phone notification that when pressed will pull up a photo from the user’s phone gallery.

I use this right after I save an image to the user’s phone but in this case you can use it to see how to find the file and then how to return to the application after viewing the photo.

To run the notification code I add a button to the view and use the on click listener to run the code to create the notification. The code to pull the image from the gallery is executed after the notification is pressed.

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.view.View;

public class MainActivity extends AppCompatActivity {

//store the current image to retrieve
int image = 0;

//increment for new multiple notifications or keep the same to overwrite previous
int notificationsID = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

//onclickListener for button on screen
public void sendNotification(View v) {
String stringURL;

//depending on the image change the file name to look for
switch (image) {
case 0:
stringURL = "/test01.jpg";
image++;
notificationsID++;
break;
default:
stringURL = "/test02.jpg";
image = 0;
notificationsID = 0;
break;
}

//get all images on phone by name
String path = "";
String absolutePathOfImage;
Uri uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
Cursor cursor = this.getContentResolver().query(uri, projection, null,
null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);

//once there is a match set it to the path
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data);
if (absolutePathOfImage.contains(stringURL)) {
path = absolutePathOfImage;
break;
}
}

cursor.close();

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this);

mBuilder.setSmallIcon(R.drawable.ic_stat_name);
mBuilder.setContentTitle("Image " + image + " Saved");
mBuilder.setContentText("Click to See!");
mBuilder.setAutoCancel(true);
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent();
resultIntent.setAction(Intent.ACTION_VIEW);
resultIntent.setDataAndType(Uri.parse("file://" + path), "image/*");

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ImageActivity.class);

// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// mId allows you to update the notification later on.
mNotificationManager.notify(notificationsID, mBuilder.build());
}
}

You will also need to declare the Activity to return to in your Manifest if you want it to return to the application when they press the notification, whether its immediately or later.

<activity android:name=".ImageActivity" android:label="@string/title_activity_image" android:parentActivityName=".MainActivity"></activity>

The image activity has no executable code, it simply calls the parent activity back to the screen for the user.

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class ImageActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 2 other subscribers