我在我的应用程序中实现了推送通知。它们在打开应用程序或在后台运行时效果很好但是当应用程序关闭时,屏幕上显示的pup up消息显示应用程序中出现问题。
这里有一段实现推送通知的代码。
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
FeedReaderDbHelperMessages mDbHelper;
/**
* Tag used on log messages.
*/
static final String TAG = "GCM Intent";
/**
* Db access object
* */
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) { // has effect of unparcelling Bundle
/*
/*
* Filter messages based on message type. Since it is likely that GCM
* will be extended in the future with new message types, just ignore
* any message types you're not interested in, or that you don't
* recognize.
*/
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification(extras);
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification(extras);
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// Post notification of received message.
sendNotification(extras);
Intent i = new Intent("android.intent.action.MAIN").putExtras(extras);
this.sendBroadcast(i);
Log.i(TAG, "Received: " + extras.toString());
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(Bundle msg) {
FacebookSdk.sdkInitialize(getApplicationContext());
Profile userProfile = Profile.getCurrentProfile();
String user_id = userProfile.getId();
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent newIntent = new Intent(this, drawnerActivity.class);
newIntent.setAction("OPEN_NOTIFICATION");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,newIntent, 0);
mDbHelper = new FeedReaderDbHelperMessages(getApplicationContext());
UpdateMessages updateMessages = new UpdateMessages(mDbHelper);
if(msg.getString("notificationType").equals("question"))
{
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo_notifica_domanda_mini)
.setContentTitle("MyApp")
.setStyle(new NotificationCompat.BigTextStyle().bigText("New question"))
.setContentText("New question");
Vibrator v = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(400);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
updateMessages.update(user_id);
}
if(msg.getString("notificationType").equals("answer"))
{
Contact userAnswer = ContactListFragment.findContactById(msg.getString("friendId"));
Timestamp notificationTimestamp = Timestamp.valueOf(msg.getString("timestamp"));
Log.i(TAG, "Answer notification received from: "+ userAnswer.getName() + " timestamp: " + notificationTimestamp.toString());
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo_mini_dialog)
.setContentTitle("MyApp")
.setStyle(new NotificationCompat.BigTextStyle().bigText("New answer"))
.setContentText(userAnswer.getName() + " answered");
Vibrator v = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
long[] pattern = {
0, // Start immediately
100,
100,
100
};
v.vibrate(pattern, -1);
//updateMessages.updateRowWithAnswer(msg.getString("questionId"), msg.getString("answer"));
FeedReaderDbHelperNotification mDbHelperNotifications = new FeedReaderDbHelperNotification(getApplicationContext());
UpdateNotifications updateNotifications = new UpdateNotifications(mDbHelperNotifications, mDbHelper);
updateNotifications.update(user_id);
//NotificationFragment.addNotification(new NotificationImpl(notificationTimestamp, 1, 0, msg.getString("questionId"), ""), getApplication().getApplicationContext());
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
}
问题是这里NullPointerException
String user_id = userProfile.getId();
。 userProfile来自com.facebook.Profile
类,因为我使用facebook sdk
来保持会话。为什么它为空?
这里有stacktrace
java.lang.NullPointerException
at com.bellantoni.chetta.lieme.GcmIntentService.sendNotification(GcmIntentService.java:80)
at com.bellantoni.chetta.lieme.GcmIntentService.onHandleIntent(GcmIntentService.java:63)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.os.HandlerThread.run(HandlerThread.java:61)
答案 0 :(得分:0)
如果此代码块是您第一次调用FacebookSdk.sdkInitialize
,则问题可能是用户尚未登录,因此没有用户配置文件,因此NullPointerException
。我有一个类似的问题,这是问题。
确保您从FacebookSDK调用Login回调。希望这会有所帮助。