从另一个包启动通知活动

时间:2017-04-14 06:08:55

标签: android push-notification android-notifications

我想将意图设置为我的MainActivity.class,它与Notification.class不同。我尝试使用ComponentName但仍为addParentStack(componentName)和NotificationManager行提供了NULL指针异常。 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference

package com.workdemos.preference;

public class Notification extends AppCompatActivity {

    public void pushNotification() {
        ComponentName componentName = new ComponentName("com.workdemos.user", "MainActivity");

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("New News")
                .setContentText("new news found in your preferred city");

        Intent resultIntent = new Intent();
        resultIntent.setComponent(componentName);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(componentName);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    }

}

更新

我尝试从调用者类发送上下文,该类位于MainActivity.class的同一个包中。

 public void pushNotification(Context context) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("New News")
            .setContentText("new news found in your preferred city");

    Intent resultIntent = new Intent();
    resultIntent.setComponent(new ComponentName(context, MainActivity.class));
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(getBaseContext());
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(resultPendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, builder.build());
}

这种方式通过了之前给我错误的行。但它没有通过addParentStack(MainActivity.class)。它仍然在该行给出了同样的错误。

2 个答案:

答案 0 :(得分:0)

ComponentName有一个构造函数,它将上下文和类作为参数。您可以使用它来获取ComponentName。

ComponentName componentName = new ComponentName(getContext(), MainActivity.class);

答案 1 :(得分:0)

通过创建一个构造函数来设置在实例启动行传递给它的上下文,我找到了一种方法来完成这项工作。

public class Notification {

    private Context context;

    public Notification(Context context) {
        this.context = context;
    }

    public void pushNotification() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("New News")
                .setContentText("new news found in your preferred city");

        Intent resultIntent = new Intent();
        resultIntent.setComponent(new ComponentName(context, MainActivity.class));

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(new ComponentName(context, MainActivity.class));
        stackBuilder.addNextIntent(resultIntent);

        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    }

}