2015年4月11日 星期六

NotificationCompat

http://segmentfault.com/a/1190000000402154
 private void sendNotification(String yourAwesomeUnicodeString) {
        Stock stock= Utils.instance.readValueAsObject(yourAwesomeUnicodeString,new Stock());
         // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, MainActivity.class);

        resultIntent.putExtra(Config.gcmDateField,yourAwesomeUnicodeString);
// 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(MainActivity.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
                );


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

                        .setContentTitle(stock.getStocktype().getName() + "缺貨通知").setSmallIcon(R.drawable.ic_launcher)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText("剩餘" + stock.getAmount())).setVibrate(new long[] { 1000, 1000 })
        .setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(true);


        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

沒有留言:

張貼留言