android推送通知无法正常工作

时间:2014-02-28 15:17:30

标签: java android push-notification broadcastreceiver google-cloud-messaging

我创建了一个使用推送通知的Android应用程序,但每当我从后端发送通知时,我的安卓平板电脑上会弹出一个对话框,说“不幸的是,lic已停止”。我尝试对应用程序进行故障排除,但似乎可以找到错误的来源。

我在我的Manifest中添加了以下权限:

<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();   
    }

    }
    }
}


} 

1 个答案:

答案 0 :(得分:0)

onReceive方法中添加该行:

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

第4行有一个空指针执行。

修改

变化:

<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" />