【问题标题】:Handle GCM notification Android处理 GCM 通知 Android
【发布时间】:2015-06-26 18:56:02
【问题描述】:

我正在开发一个应用程序,它接收 GCM 通知以提醒用户有关促销和类似的东西。

到目前为止,我已经设法发送和接收通知。这是我的代码:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ComponentName comp = new ComponentName(context.getPackageName(), GCMNotificationIntentService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
}

}

public class GCMNotificationIntentService extends IntentService {
public static final int notifyID = 1337;
NotificationCompat.Builder builder;

public GCMNotificationIntentService() {
    super("GcmIntentService");
}

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

    String messageType = gcm.getMessageType(intent);
    if (!extras.isEmpty()) {
       sendNotification(extras.getString("type"),extras.getString("msg"));
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String type, String msg) {
    Intent resultIntent = new Intent(this, SplashActivity.class);
    resultIntent.putExtra("type", type);
    resultIntent.putExtra("msg", msg);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,resultIntent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder mNotifyBuilder;
    NotificationManager mNotificationManager;

    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    mNotifyBuilder = new NotificationCompat.Builder(this)      
            .setContentTitle("x")
            .setContentText(msg)
            .setSmallIcon(R.drawable.ic_launcher);
    mNotifyBuilder.setContentIntent(resultPendingIntent);

    int defaults = 0;
    defaults = defaults | Notification.DEFAULT_LIGHTS;
    defaults = defaults | Notification.DEFAULT_VIBRATE;
    defaults = defaults | Notification.DEFAULT_SOUND;

    mNotifyBuilder.setDefaults(defaults);
    mNotifyBuilder.setAutoCancel(true);
    mNotificationManager.notify(notifyID, mNotifyBuilder.build());
}

}

问题是我不知道如何(或在哪里)显示消息警报。

在网上搜索,我发现了一个实现,即使应用程序关闭也会显示消息,但我只需要在应用程序上显示消息,例如:当应用程序打开时或应用程序关闭时然后用户打开它。

【问题讨论】:

    标签: android android-intent notifications google-cloud-messaging android-notifications


    【解决方案1】:

    Android 通知框架旨在显示通知,无论您的应用是否打开。通知显示在顶部的通知栏中,不会显示在您的活动屏幕中。

    如果您不想在应用程序未运行时显示通知,您可以执行一些操作,例如设置一个变量 isRunning,并在活动的 onResume() 中将其设置为 true,在活动的 onPause() 中将其设置为 false。这样,您现在可以参考来了解应用程序是否正在运行。如果 isRunning 为 false,那么您可以将在后台收到的通知保存到 SharedPreferences 或数据库中 - 您无需使用 Notification 框架即可执行此操作。如果应用程序已打开,则您可以显示消息警报 - 如果您打算这样做,则使用通知框架或使用 AlertDialog。

    【讨论】:

    • 好的,谢谢您的信息。如何以简单的方式在我的所有活动中实现变量“isRunning”?
    • 您可以使用 SharedPreferences 在不同的会话中保留变量。或者您可以将值存储在数据库中并每次都检索它,但 SharedPreferences 会更容易
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多