gpt4 book ai didi

android - 单击 GCM 通知未在 Android Kitkat 中打开所需的 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:31:45 24 4
gpt4 key购买 nike

我正在做一个需要我向我的用户发送推送通知的应用。

我正在使用 this example .首先,我在点击通知栏上的通知时遇到了问题,它没有打开所需的 Activity ,但现在已经解决了。我在应用处于 Activity 状态或在后台时收到通知。

但是我的一部手机(Nexus 4,它有 Android Kitkat 4.4)表现异常:当应用程序在前台并显示消息时,它通过广播显示通知,但没有启动点击通知时需要启动的 Activity在通知栏上。有时它不会像我的其他手机那样收到通知。

package com.androidhive.pushnotifications;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gcm.GCMBaseIntentService;

import static com.androidhive.pushnotifications.CommonUtilities.SENDER_ID;
import static com.androidhive.pushnotifications.CommonUtilities.displayMessage;
import android.database.sqlite.SQLiteDatabase;

public class GCMIntentService extends GCMBaseIntentService {
private static SQLiteDatabase sampleDB;
private static final String SAMPLE_DB_NAME = "pushData.db";
private static final String SAMPLE_TABLE_NAME = "pushtabledata";
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(SENDER_ID);

}

/**
* Method called on device registered
**/
@Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "Device registered: regId = " + registrationId);
displayMessage(context, "Your device registred with GCM");
Log.d("NAME", MainActivity.name);
ServerUtilities.register(context, MainActivity.name, MainActivity.email, registrationId);
}

/**
* Method called on device un registred
* */
@Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "Device unregistered");
displayMessage(context, getString(R.string.gcm_unregistered));
ServerUtilities.unregister(context, registrationId);
}

/**
* Method called on Receiving a new message
* */


@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("price");
Log.d("GCMIntentService", "here");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}

/**
* Method called on receiving a deleted message
* */
@Override
protected void onDeletedMessages(Context context, int total) {
Log.i(TAG, "Received deleted messages notification");
String message = getString(R.string.gcm_deleted, total);
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}

/**
* Method called on Error
* */
@Override
public void onError(Context context, String errorId) {
Log.i(TAG, "Received error: " + errorId);
displayMessage(context, getString(R.string.gcm_error, errorId));
}

@Override
protected boolean onRecoverableError(Context context, String errorId) {
// log message
Log.i(TAG, "Received recoverable error: " + errorId);
displayMessage(context, getString(R.string.gcm_recoverable_error,
errorId));
return super.onRecoverableError(context, errorId);
}

/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message) {
/*NotificationManager mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);

Intent inty = new Intent(context, MainActivity.class);
inty.putExtra("message", message);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
inty, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setContentText(message);

mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(0, mBuilder.build());*/
// Log.d("in GCM Intent", message);
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
StringBuffer sb = new StringBuffer();
if(message != null){
Log.d("in GCM Intent", "not null");
if(message.contains("@@@@@@@@@@@@"))
{

Log.d("in GCM Intent", "got @");
String []parts = message.split("@@@@@@@@@@@@");

Log.d("After : separate", parts[0] + "" + parts[1]);
sb.append(parts[0]);
sampleDB= context.openOrCreateDatabase(SAMPLE_DB_NAME, SQLiteDatabase.OPEN_READWRITE, null);
ContentValues values = new ContentValues();
values.put("title", parts[0]);
DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm");
String date = df.format(Calendar.getInstance().getTime());
values.put("time", date);
values.put("message", parts[1]);
try{
sampleDB.beginTransaction();
sampleDB.insert(SAMPLE_TABLE_NAME, null, values);
sampleDB.setTransactionSuccessful();

}
catch(Exception e)
{
Log.d("Exception",e.toString());
}
finally{
sampleDB.endTransaction();
}
sampleDB.close();
}
else
{
sb.append(message);
}
}
else
{
sb.append("No data");
}
String title = context.getString(R.string.app_name);
notification.when = when;
notification.icon = icon;
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra("message", message);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, sb.toString(), intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;

// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;

// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}

}

我尝试用谷歌搜索这个问题,并找到了一些数据,如下所示,但没有任何意义。

link1

link2

link3

已编辑:- 我已按照要求粘贴了 GCMIntentService 的代码。

最佳答案

此问题在 Android Kitkat 4.4 和 Lollipop 中已知。请添加:

android:exported="true"

在 android manifest 里面的 activity 标签中,它应该在点击状态栏的通知后打开。

此外,由于 Google Play 服务已更新,因此请将 Google Play 服务库导入您的项目,而不是导入 gcm.jar。

关于android - 单击 GCM 通知未在 Android Kitkat 中打开所需的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21330136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com