Java android推送通知不工作
我创建了一个使用推送通知的android应用程序,但每次我从后端发送通知时,我的android平板电脑上都会弹出一个对话框,说“不幸的是,lic已经停止”。我尝试对应用程序进行故障排除,但似乎可以找到错误的来源 我已在清单中添加了以下权限:Java android推送通知不工作,java,android,push-notification,broadcastreceiver,google-cloud-messaging,Java,Android,Push Notification,Broadcastreceiver,Google Cloud Messaging,我创建了一个使用推送通知的android应用程序,但每次我从后端发送通知时,我的android平板电脑上都会弹出一个对话框,说“不幸的是,lic已经停止”。我尝试对应用程序进行故障排除,但似乎可以找到错误的来源 我已在清单中添加了以下权限: <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <permission android:name="com.example.
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.example.myappname.pemermission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.myappname.permission.C2D_MESSAGE" />
我的Broadcastreceiver
代码如下:
public class GcmBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg) {
String sh = gcm.getMessageType(arg);
Bundle bb= arg.getExtras();
if(!bb.isEmpty()){
if(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(sh)){
String b=arg.getExtras().getString("message")+"<br>";
String f="handler";
try {
FileOutputStream fos=openFileOutput(f, con.MODE_APPEND);
fos.write(b.getBytes());
fos.close();
} catch (FileNotFoundException ef) {
error(ef.toString());
ef.printStackTrace();
} catch (IOException e) {
error(e.toString());
e.printStackTrace();
}
catch(Exception ex){
error(ex.toString());
}
sendNoti("you have a message/s");//this sends notification to user
try{
}
catch(Exception e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}
}
}
}
公共类GcmBroadcastReceiver扩展了BroadcastReceiver{
@凌驾
公共void onReceive(上下文arg0,意图arg){
字符串sh=gcm.getMessageType(arg);
Bundle bb=arg.getExtras();
如果(!bb.isEmpty()){
if(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(sh)){
字符串b=arg.getExtras().getString(“消息”)+“
”;
字符串f=“handler”;
试一试{
FileOutputStream fos=openFileOutput(f,con.MODE_-APPEND);
fos.write(b.getBytes());
fos.close();
}捕获(FileNotFoundException ef){
错误(ef.toString());
ef.printStackTrace();
}捕获(IOE异常){
错误(例如toString());
e、 printStackTrace();
}
捕获(例外情况除外){
错误(例如toString());
}
sendNoti(“您有一条消息”);//这会向用户发送通知
试一试{
}
捕获(例外e){
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}
}
}
}
}
在您的onReceive
方法中添加该行:
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
第四行有一个空指针执行选项
编辑:
更改:
<activity android:name="noti_handler"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden" />
致:
及
致:
post logcat buddy…..我使用的是设备而不是模拟器,我似乎无法调试我的三星galaxy tab3上的应用程序-无法从三星网站获取oem驱动程序我已经在oncreate方法中完成了这项工作,我只是将其放在我的GcmBroadcastReceiver类中,但每次我向我的用户发送推送通知时,仍然会显示相同的对话框devicePlease发布整个类。使更改仍然无效更改我的noti_处理程序活动的名称导致清单文件中的错误应以“>”而不是“/>”结尾。我是否仍发布整个类?真的需要让这个应用程序工作吗
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
<activity android:name="noti_handler"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden" />
<activity android:name="com.example.myappname.noti_handler"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden" />
<permission
android:name="com.example.myappname.pemermission.C2D_MESSAGE"
android:protectionLevel="signature" />
<permission
android:name="com.example.myappname.permission.C2D_MESSAGE"
android:protectionLevel="signature" />