Skip to content

Instantly share code, notes, and snippets.

@pokk
Last active December 16, 2016 07:43
Show Gist options
  • Save pokk/adf859b0525fba3a2c6e8b723220a40c to your computer and use it in GitHub Desktop.
Save pokk/adf859b0525fba3a2c6e8b723220a40c to your computer and use it in GitHub Desktop.
The way to do a notification.

Introduction

The way of using android notification.

public class MyBroadcastReceiver extends BroadcastReceiver
{
public static final String BROADCAST_NAME = "sample.receiver";
@Override
public void onReceive(Context context, Intent intent)
{
Intent startIntent = this.isAppOnForeground(context) ?
new Intent(RECEIVER_NAME) /* Send a msg to local receiver */ :
new Intent(context, SampleActivity.class) /* Which activity you will start from */;
// Put a argments.
startIntent.putExtra("param1", "test parameter");
if (this.isAppOnForeground(context))
{
// Send to specific activity with argments.
context.sendBroadcast(startIntent);
}
else
{
// Start a activity as a new task.
startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startIntent);
}
}
private boolean isAppOnForeground(Context context)
{
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses != null)
{
final String packageName = context.getPackageName();
for (RunningAppProcessInfo appProcess : appProcesses)
{
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&
appProcess.processName.equals(packageName))
{
return true;
}
}
}
return false;
}
}
public static final String SAMPLE_BROADCAST = "sample.broadcast"
public static final String ARUG_PARAMETER = "param1"
public static final int NOTIFICATION_ID = 809;
public static final int BROADCAST_ID = 938;
private void showNotification() {
// To SAMPLE_BROADCAST BroadcastReceiver.
Intent intent = new Intent(SAMPLE_BROADCAST);
intent.putExtra(ARUG_PARAMETER, "Hello World!");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context
, NOTIFICATION_ID
, intent
, PendingIntent.FLAG_UPDATE_CURREN);
// Create a notification.
notification = createNotification();
// Push a notification.
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
.notify(NOTIFICATION_ID /* ID of notification */, notification);
}
private Notification createNotification() {
return new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.sample_icon)
.setContentTitle("Title")
.setContentText("Body context")
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent)
.builde();
}
private Notification createUnclearableNotification() {
return new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.sample_icon)
.setContentTitle("Title")
.setContentText("Body context")
// Can't clearable flag.
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.build();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment