简体   繁体   English

Android GCM推送通知当机

[英]android GCM Push notification crashing

I am working on android GCM to display some notification. 我正在android GCM上显示一些通知。 It works fine. 工作正常。 But when I add a code that could enable me to start the Home activity upon clicking the notification, it crashes. 但是,当我添加一个使我可以在单击通知时启动Home活动的代码时,它会崩溃。 If I remove the PendingIntent It works fine but with no action. 如果我删除PendingIntent,它可以正常工作,但不执行任何操作。

Below is the code I am using 下面是我正在使用的代码

public class GcmMessageHandler extends GcmListenerService {
public static final int MESSAGE_NOTIFICATION_ID = 435345;
Intent intent = new Intent(GcmMessageHandler.this, HomeActivity.class);

int requestID = (int) System.currentTimeMillis(); //unique requestID to differentiate between various notification with same NotifId
int flags = PendingIntent.FLAG_CANCEL_CURRENT; // cancel old intent and create new one
PendingIntent pIntent = PendingIntent.getActivity(this, requestID, intent, flags);

@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");

    createNotification(from, message);
}

// Creates notification based on title and body received
private void createNotification(String title, String body) {
    Context context = getBaseContext();

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(body)
            .setContentIntent(pIntent);
    mBuilder.setAutoCancel(true);

    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
}

} }

and below is the error log I am getting. 下面是我得到的错误日志。

04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: FATAL EXCEPTION: main
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: Process: abenakebe.yournet.com.abenakebe, PID: 14201
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate service abenakebe.yournet.com.abenakebe.utils.GcmMessageHandler: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at android.app.ActivityThread.handleCreateService(ActivityThread.java:3623)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at android.app.ActivityThread.access$2000(ActivityThread.java:198)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1759)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6837)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:101)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at android.app.PendingIntent.getActivity(PendingIntent.java:286)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at android.app.PendingIntent.getActivity(PendingIntent.java:252)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at abenakebe.yournet.com.abenakebe.utils.GcmMessageHandler.<init>(GcmMessageHandler.java:25)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at java.lang.reflect.Constructor.newInstance(Native Method)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at java.lang.Class.newInstance(Class.java:1684)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at android.app.ActivityThread.handleCreateService(ActivityThread.java:3620)
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at android.app.ActivityThread.access$2000(ActivityThread.java:198) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1759) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6837) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

You should move this two things at least to onCreate() or onMessageReceived() 您应该至少将这两件事移动到onCreate()onMessageReceived()

Intent intent = new Intent(GcmMessageHandler.this, HomeActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, requestID, intent, flags);

It crashes because this doesn't have Context before onCreate was called. 它崩溃,因为this没有Context之前onCreate被调用。

SHould be like this: 应该是这样的:

public class GcmMessageHandler extends GcmListenerService {
    public static final int MESSAGE_NOTIFICATION_ID = 435345;
    Intent intent;

    int requestID = (int) System.currentTimeMillis(); //unique requestID to differentiate between various notification with same NotifId
    int flags = PendingIntent.FLAG_CANCEL_CURRENT; // cancel old intent and create new one
    PendingIntent pIntent;

    @Override
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        intent = new Intent(GcmMessageHandler.this, HomeActivity.class);
        pIntent = PendingIntent.getActivity(this, requestID, intent, flags);
        createNotification(from, message);
    }

    // Creates notification based on title and body received
    private void createNotification(String title, String body) {
        Context context = getBaseContext();

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(body)
                .setContentIntent(pIntent);
        mBuilder.setAutoCancel(true);

        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM