通知意图 未启动 Actiivty 安卓



我正在尝试在通知单击时启动活动,但它从未启动。以下是我发送通知的代码:

Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.putExtra("Notification", type);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
        Notification n = new Notification.Builder(this)
                .setContentTitle(title)
                .setContentText(type)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pIntent)
                .build();
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, n);

我从来没有在点击通知时接到该活动的电话。

试试这个
Splash_Activity.class替换为您的活动,并确保已在Manifest.xml中注册了此类

 protected void displayNotification(Context context, String[] strContent) {
            try {
                int numMessages = 0;
                /* Invoking the default notification service */
                NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(this);
                mBuilder.setContentTitle("New "+strContent[0].trim());
                mBuilder.setContentText("You've received new "+strContent[0].trim());
                mBuilder.setTicker("New "+strContent[0].trim()+" Alert!");
                mBuilder.setSmallIcon(R.drawable.ic_notif);
                mBuilder.setAutoCancel(true);
                mBuilder.setColor(context.getResources().getColor(R.color.ColorPrimary));
                mBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
                mBuilder.setDefaults(Notification.DEFAULT_ALL);
                mBuilder.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000});         
                mBuilder.setLights(Color.RED, 3000, 3000);
             mBuilder.setNumber(++numMessages);
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
                String[] events = new String[strContent.length-1];
                for(int i=0; i<strContent.length-1; i++){
                    events[i] = new String(strContent[i+1].trim());
                }
                inboxStyle.setBigContentTitle("New "+strContent[0].trim()+" received!");
                for (int i=0; i < events.length; i++) {
                    inboxStyle.addLine(events[i]);
                }
                mBuilder.setStyle(inboxStyle);
                /* Creates an explicit intent for an Activity in your app */
                Intent resultIntent = new Intent(this, Splash_Activity.class);

                TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
                stackBuilder.addParentStack(Splash_Activity.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);
                mBuilder.setContentIntent(resultPendingIntent);
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                /* notificationID allows you to update the notification later on. */
                mNotificationManager.notify(9999, mBuilder.build());
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }

当你想要通知时调用这样的方法

public void showNotification() {
        PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, ShowNotificationDetailActivity.class), 0);
        Resources r = getResources();
        Notification notification = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.main)
                .setContentTitle("title")
                .setContentText("Welcome")
                .setContentIntent(pi)
                .setAutoCancel(true)
                .build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, notification);
    }

创建一个这样的类,并让你的班级打算进入活动

public class ShowNotificationDetailActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(this, Main.class);
        startActivity(intent);
    }
}

相关内容

  • 没有找到相关文章

最新更新