广播接收器无法通知我的通知

时间:2014-07-17 22:01:27

标签: android

我已经搜索了很多东西,即使遵循了很多教程,也似乎无法找到解决方案。

Intent testIntent = new Intent(this, ActionFromNotification.class);
        PendingIntent pendIntent = PendingIntent.getBroadcast(this, 0, testIntent,       PendingIntent.FLAG_UPDATE_CURRENT); 

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);


          builder.setSmallIcon(R.drawable.pulp_orange_icon)
          .setContentTitle("Pulp")
          .setContentText("Hi")
          .setStyle(new NotificationCompat.BigTextStyle().bigText("hello"))
          .addAction(R.drawable.ic_launcher, "testbutton1",pendIntent)
          .setTicker("TICKER").setContentTitle("TITLE").setContentText("CONTENT")
                    .setWhen(System.currentTimeMillis()).setAutoCancel(false)
                    .setOngoing(true).setPriority(Notification.PRIORITY_HIGH);
                   // .setContentIntent(pendIntent);
            Notification mNotification = builder.build();
        startForeground(1337, mNotification);




public class ActionFromNotification extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent){
            Log.d("BYI", "end button pressed from notification");
        }
    }

//Manifest File
<receiver android:name=".ActionFromNotification"/>

当我点击&#34; testbutton1&#34;在通知中(显示正确),我应该得到一个log.d响应...但我什么都没看到。

3 个答案:

答案 0 :(得分:4)

我认为你需要做的是用一个动作而不是一个类创建你的初始意图,如:

Intent testIntent = new Intent("com.example.my_project.test_intent");

然后设置你的PendingIntent:

PendingIntent pendIntent = PendingIntent.getBroadcast(this, 0, testIntent,       PendingIntent.FLAG_UPDATE_CURRENT);

在你的清单中设置一个意图过滤器来触发你的接收器

<receiver android:name=".ActionFromNotification">   // note the removal of the / in />
   <intent-filter>
      <action android:name="com.example.my_project.test_intent" />
   </intent-filter>
</receiver>

重新阅读接收器标签的doc之后,似乎它也应该使用类名,在这种情况下,你应该尝试在原始接收器标签上使用类的全名,即&#34; fullly_qualified_pa​​ckage_name.ActionFromNotification&#34;

答案 1 :(得分:0)

对于GCM,我做了以下事情:

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;

/**
 * Created by Leonardo on 07/07/2014.
 */
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

请注意,我使用了wakefulBroadcastReceiver。尽管如此,强烈建议不要这样做。

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

import com.google.android.gms.gcm.GoogleCloudMessaging;

/**
 * Created by Leonardo on 07/07/2014.
 */
public class GcmIntentService extends IntentService {

    public static final int NOTIFICATION_ID = 1;
    private NotificationManager notificationManager;
    NotificationCompat.Builder builder;

    public GcmIntentService() {
        super("GCM Intent Service");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification(extras);
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification(extras);
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                sendNotification(extras);
            }
        }
        GcmBroadcastReceiver.completeWakefulIntent(intent);

    }

    private void sendNotification(Bundle msg) {
        notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, FriendActivity.class), 0);
        NotificationCompat.Builder mBuilder;

        mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_coin_bw).setContentTitle("Friend Request")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg.getString("name"))).setContentText(msg.getString("email")).
                        setLights(Color.YELLOW, 500, 1000).setAutoCancel(true);


        mBuilder.setContentIntent(contentIntent);
        notificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}

你的清单必须有:

<receiver
            android:name="YOUR_RECEIVER_CLASS_NAME"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <category android:name="YOUR_FULL_PACKAGE_NAME" />
            </intent-filter>
        </receiver>

        <service android:name="YOUR_SERVICE_CLASS_NAME" />
 <uses-permission android:name="com.maddogs.mymoney.permission.C2D_MESSAGE" />

    <permission
        android:name="com.maddogs.mymoney.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

看看它是否有帮助!

答案 2 :(得分:0)

好的,这就是我做的。我使用了nPn的答案组合,不得不进行一些调整。

我按照他的建议做了我的意图。

Intent testIntent = new Intent("com.pulp.action");
        pendIntent = PendingIntent.getBroadcast(this, 0, testIntent, 0); 

这是我的新清单......必须定义整个类路径并忘记内部类$符号。所以我的愚蠢错误!

<receiver android:name="com.pulp.LocationTrackingService$ActionFromNotification">
<intent-filter>
    <action android:name="com.pulp.action"/>
</intent-filter>
</receiver>
相关问题