即使应用程序在android中关闭,也会推送通知

时间:2015-08-17 09:20:16

标签: android notifications

我正在从我的应用程序推送通知并每隔一分钟刷新一次通知方法。应用程序打开时会显示通知。即使应用程序关闭,如何推送通知?

public void CustomNotification(String title_notify,String image_notify) {
    // Using RemoteViews to bind custom layouts into Notification
    RemoteViews remoteViews = new RemoteViews(getPackageName(),
            R.layout.customnotification);

    // Set Notification Title
    String strtitle = title_notify;
    // Set Notification Text
    String strtext = "";

    // Open NotificationView Class on Notification Click
    Intent intent = new Intent(this, Notification_ListView.class);
    // Send data to NotificationView Class
    intent.putExtra("title", strtitle);

    intent.putExtra("location", location);
    intent.putExtra("text", strtext);
    intent.putExtra("user_id", user_id);
    intent.putExtra("country_id", country_id);
    // Open NotificationView.java Activity
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    bitmap = getBitmapFromURL(image_notify);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            // Set Icon
            .setSmallIcon(getNotificationIcon())
            // Set Ticker Message  
            .setTicker("")
            // Dismiss Notification
            .setAutoCancel(true)
            // Set PendingIntent into Notification
            .setContentIntent(pIntent)
            // Set RemoteViews into Notification
            .setContent(remoteViews);

    // Locate and set the Image into customnotificationtext.xml ImageViews
    remoteViews.setImageViewBitmap(R.id.imagenotileft,bitmap);
    //remoteViews.setImageViewResource(R.id.imagenotiright,R.drawable.androidhappy);

    // Locate and set the Text into customnotificationtext.xml TextViews
    remoteViews.setTextViewText(R.id.title,strtitle);
    remoteViews.setTextViewText(R.id.text,strtext);

    Random random = new Random();
    int m = random.nextInt(9999 - 1000) + 1000;
    // Create Notification Manager
    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Build Notification with Notification Manager
    notificationmanager.notify(m, builder.build());

}

1 个答案:

答案 0 :(得分:1)

试试此示例push notification