Android通知(应用程序打开时)

时间:2014-09-04 16:44:18

标签: android notifications android-service android-notifications

我在android服务中使用此代码发送通知:

notification.setLatestEventInfo(this, "title", "text", pendingIntent); 
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(id, notification);

如果应用程序关闭,此代码可以正常工作,但我也希望它在应用程序(应用程序的任何活动)打开时发送通知,当应用程序打开时,我没有收到通知。因此,即使应用程序处于打开状态,请告诉我如何接收通知。

2 个答案:

答案 0 :(得分:0)

public static final int FLAG_ONGOING_EVENT

自:API级别1 如果此通知引用正在进行的操作(如电话呼叫),则应将位按位进入标记字段,该字段应设置。

public static final int FLAG_FOREGROUND_SERVICE

从以下版本开始:API Level 5要按位进入flags字段,如果此通知代表当前正在运行的服务,则应设置该字段。

尝试使用:

notification.flags |= Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_NO_CLEAR

答案 1 :(得分:0)

不适合我...... 与

private void sendNotification(String trmMsg,String user, int role) {
    Intent resultIntent = new Intent(this, MainActivity.class);
    resultIntent.putExtra("trmjson", trmMsg);
    resultIntent.putExtra("fragment", "readN");
    resultIntent.setAction(Intent.ACTION_MAIN);
    resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,resultIntent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder mNotifyBuilder;
    NotificationManager mNotificationManager;

    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    String msg = role == 1 ? user + " conferma il cambio di turno!" : "Attenzione, cambio di turno!";

    mNotifyBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("TRM")
            .setContentText(msg)
            .setSmallIcon(R.drawable.ic_notific);
    mNotifyBuilder.setContentIntent(resultPendingIntent);

    int defaults = 0;
    defaults = defaults | Notification.DEFAULT_LIGHTS;
    defaults = defaults | Notification.DEFAULT_VIBRATE;
    defaults = defaults | Notification.DEFAULT_SOUND;

    mNotifyBuilder.setDefaults(defaults);
    mNotifyBuilder.setContentText(msg);
    mNotifyBuilder.setAutoCancel(true);
    mNotificationManager.notify(notifyID, mNotifyBuilder.build());
}

通知仍然存在,并且在点击后不会消失。

我已经按照本教程http://programmerguru.com/android-tutorial/how-to-send-push-notifications-using-gcm-service/进行了一切正常但是当应用程序打开后我点击了notific到达时,没有任何内容出现....

{{1}}