我正在为我的项目使用cordova。单击推送通知消息后,我的应用程序崩溃。我在log cat中收到此错误消息。
03-12 10:53:21.185: W/PluginManager(5548): THREAD WARNING: exec() call to PushPlugin.register blocked the main thread for 23ms. Plugin should use CordovaInterface.getThreadPool().
03-12 10:53:21.185: D/WebView(5548): loadUrlImpl: called
03-12 10:53:21.280: D/dalvikvm(5548): GC_CONCURRENT freed 581K, 12% free 12778K/14407K, paused 11ms+2ms, total 26ms
03-12 10:53:21.290: D/AndroidRuntime(5548): Shutting down VM
03-12 10:53:21.290: W/dalvikvm(5548): threadid=1: thread exiting with uncaught exception (group=0x40fc72a0)
03-12 10:53:21.290: E/AndroidRuntime(5548): FATAL EXCEPTION: main
03-12 10:53:21.290: E/AndroidRuntime(5548): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41d42d38 is not valid; is your activity running?
03-12 10:53:21.290: E/AndroidRuntime(5548): at android.view.ViewRootImpl.setView(ViewRootImpl.java:708)
03-12 10:53:21.290: E/AndroidRuntime(5548): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:346)
03-12 10:53:21.290: E/AndroidRuntime(5548): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
03-12 10:53:21.290: E/AndroidRuntime(5548): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
客户端
GCMIntentService.java
package in.amolgupta.android.gcm;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
/**
* {@link IntentService} responsible for handling GCM messages.
*/
public class GCMIntentService extends GCMBaseIntentService {
@SuppressWarnings("hiding")
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super("xxxxxxxxxxxxx");
}
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message) {
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context,
SamplePushActivity.class);
// 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, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
@Override
protected void onError(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
Log.d("GCM", "RECIEVED A MESSAGE");
// Get the data from intent and send to notificaion bar
generateNotification(arg0, arg1.getStringExtra("message"));
}
@Override
protected void onRegistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
}
SamplePushActivity.java
package in.amolgupta.android.gcm;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import com.google.android.gcm.GCMRegistrar;
public class SamplePushActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView regNumber = (TextView)findViewById(R.id.textView1);
EditText regNO = (EditText)findViewById(R.id.editText1);
GCMRegistrar.checkDevice(this);
//uncomment below lines to unregister the device
// GCMRegistrar.unregister(this);
// Log.d("info",
// "unregistereddd....." + GCMRegistrar.getRegistrationId(this));
GCMRegistrar.checkManifest(this);
if (GCMRegistrar.isRegistered(this)) {
Log.d("info", GCMRegistrar.getRegistrationId(this));
}
final String regId = GCMRegistrar.getRegistrationId(this);
regNumber.setText(regId);
regNO.setText(regId);
if (regId.equals("")) {
// replace this with the project ID
GCMRegistrar.register(this, "xxxxxxxxxxxxx");
Log.d("info", GCMRegistrar.getRegistrationId(this));
regNumber.setText(regId);
regNO.setText(regId);
} else {
Log.d("info", "already registered as" + regId);
}
}
}
服务器端
Notify.java
package in.amolgupta.android.gcm.server;
import java.util.ArrayList;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Sender;
class Notify {
public static void main(String args[]) {
try {
Sender sender = new Sender(
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
ArrayList<String> devicesList = new ArrayList<String>();
devicesList.add("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
// devicesList.add("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
devicesList.add("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzz");
// Use this line to send message without payload data
// Message message = new Message.Builder().build();
// use this line to send message with payload data
Message message = new Message.Builder()
.collapseKey("1")
.timeToLive(3)
.delayWhileIdle(true)
.addData("message",
"this text will be seen in notification bar!!")
.build();
// Use this code to send to a single device
// Result result = sender
// .send(message,
// "APA91bGiRaramjyohc2lKjAgFGpzBwtEmI8tJC30O89C2b3IjP1CuMeU1h9LMjKhmWuZwcXZjy1eqC4cE0tWBNt61Kx_SuMF6awzIt8WNq_4AfwflaVPHQ0wYHG_UX3snjp_U-5kJkmysdRlN6T8xChB1n3DtIq98w",
// 1);
// Use this for multicast messages
MulticastResult result = sender.send(message, devicesList, 1);
sender.send(message, devicesList, 1);
System.out.println(result.toString());
if (result.getResults() != null) {
int canonicalRegId = result.getCanonicalIds();
if (canonicalRegId != 0) {
}
} else {
int error = result.getFailure();
System.out.println(error);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
您可以参考此网站。我正在为我的项目使用她的源代码。
答案 0 :(得分:0)
检查一下:
Show activity after notification Android
此外,将 android:exported =&#34; true&#34; 添加到您的清单文件中的活动中。
希望这有帮助。