单击通知时,不会显示通知页面

时间:2015-10-15 20:09:43

标签: android notifications push-notification android-notifications

我因为问我这个问题而感到羞耻,因为我知道的更好,但是现在我很困难!我编写了代码,用于在单击通知时调用活动。但是,加班我点击通知,活动没有开始,而是通知就消失了。我有一个Receiver.java,它扩展了BroadcastReceiver,然后调用包含我设置的通知构建器的Alarm Service。这是我使用的通知构建器:

Intent intent1=new Intent(Alarm.this,NotificationPage.class);
    PendingIntent pendingIntent=PendingIntent.getActivity(Alarm.this,1,intent1,0);

    NotificationCompat.Builder builder=new NotificationCompat.Builder(Alarm.this);
    builder.setAutoCancel(true);
    builder.setContentTitle(from);
    builder.setContentText(message);
    builder.setSmallIcon(R.drawable.icon);
    builder.setContentIntent(pendingIntent);
    builder.setSound(sound);
    builder.build();
    nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notify=builder.getNotification();
    notify.flags=Notification.FLAG_AUTO_CANCEL;
    nm.notify(0, notify);

除了通知页面弹出之外的所有内容都有效。我在设备上测试了它,应用程序没有崩溃所以我知道没有错误。 请帮忙!

沮丧的程序员atm

1 个答案:

答案 0 :(得分:1)

public void notifyIncomingRequest(Context context,String message)
{
    Intent intent=new Intent();
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    intent.putExtra("notification", true);
    intent.putExtra("message", message);
    intent.setAction(Long.toString(System.currentTimeMillis()));
    intent.setClass(context, YOURCLASS.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(context,0,intent, PendingIntent.FLAG_UPDATE_CURRENT);



    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true);

    boolean notification = true;
    boolean notification_vibration = false;
    boolean notification_sound = true;



    String title = "YOURTITLE";
    mBuilder.setContentTitle(title);
    String msg=message;
    mBuilder.setContentText(msg);
    mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(title));


    if(notification){

        mBuilder.setContentIntent(contentIntent);
        Notification notify = mBuilder.build();

        if(notification_vibration)
            notify.defaults |= Notification.DEFAULT_VIBRATE;

        if(notification_sound)
            notify.defaults |= Notification.DEFAULT_SOUND;

        mNotificationManager.notify(Integer.valueOf(intent.getIntExtra("number", 13)), notify);
    }
}

YOURCLASS.class是您希望在单击通知后显示的活动,尝试使用适当的上下文和消息调用此函数,您可以继续使用。