Skip to content

Instantly share code, notes, and snippets.

@Aiur3908
Created April 22, 2015 02:57
Show Gist options
  • Save Aiur3908/2d4db51e8ab0eda5b7b4 to your computer and use it in GitHub Desktop.
Save Aiur3908/2d4db51e8ab0eda5b7b4 to your computer and use it in GitHub Desktop.
sendNotification
private void sendNotification()
{
int notificationId = 001;
//決定這個notification的辨識編號
Intent intent = new Intent(this , MainActivity.class);
//建立Intent , 並決定要開啟哪個class
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, intent, 0);
//使用PendingIntent來等待執行這個intent
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("內容")
.setContentIntent(pendingIntent);
//設定ContentIntent為剛才建立的pendingIntent
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
//取得NotificationManagerCompat的實體
notificationManager.notify(notificationId, notificationBuilder.build());
//填入辨識編號及notification後,即可發送出去
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment