我創建了一個使用推送通知的android應用程序,但每次我發送後端通知時,我的android平板電腦上會彈出一個對話框,說「不幸已經停止了」。我嘗試瞭解應用程序的故障,但似乎得到錯誤來自哪裏。android推送通知不起作用
我已在我的清單以下權限:
<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" />
我也註冊了一個接收器來處理消息:
<receiver
android:name="com.example.myappname.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.myappname"/>
</intent-filter>
</receiver>
最後,我宣佈一個新的meta-data
標籤:
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
<activity android:name="noti_handler"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden" />
我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();
}
}
}
}
}
後logcat的好友..... –
我是從設備工作不仿真器,我似乎無法調試我的三星Galaxy應用tab3-不能得到三星網站 –